-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_call.py
More file actions
114 lines (93 loc) · 4.16 KB
/
Copy pathllm_call.py
File metadata and controls
114 lines (93 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from dotenv import load_dotenv
import os
import json
load_dotenv()
def get_chat_response(messages, context_data):
"""
Get a response from the LLM based on the chat history and code context.
Args:
messages: List of dictionaries [{'role': 'user'|'assistant', 'content': '...'}]
context_data: Dictionary containing line_content, line_number, variables, context_code
"""
api_key = os.getenv("KEYWORDS_AI_API_KEY")
if not api_key:
return "Error: KEYWORDS_AI_API_KEY not found in .env file."
llm = ChatOpenAI(
base_url="https://api.keywordsai.co/api/",
api_key=api_key,
model="gpt-4o-mini",
streaming=False,
)
# Construct System Prompt
line_content = context_data.get('line_content', '')
line_number = context_data.get('line_number', 0)
variables = context_data.get('variables', {})
stack = context_data.get('stack', [])
stdout = context_data.get('stdout', '')
exec_count = context_data.get('exec_count', 0)
context_code = context_data.get('context_code', '')
error_message = context_data.get('error_message')
if error_message:
system_prompt = f"""You are a friendly and simplified coding assistant for non-technical users.
CRITICAL ERROR DETECTED:
The user's code has failed to run due to the following error:
{error_message}
Execution State at Crash:
--------------------------
Line {line_number}: {line_content}
Execution Count: {exec_count}
Call Stack:
{json.dumps(stack, indent=2) if stack else "Empty"}
Output So Far:
{stdout}
Full Source Code:
{context_code if context_code else "Not provided"}
Your Goal:
1. Explain the error in extremely simple, non-technical language (avoid jargon like "traceback", "stack frame", "syntax error" unless defined simply).
2. Use analogies if helpful (e.g., "The computer got confused because it expected a number but got a word").
3. Point out exactly where the mistake is.
4. Provide a clear, step-by-step fix.
5. Be concise and efficient. Do not write long paragraphs.
"""
else:
system_prompt = f"""You are a friendly and simplified coding assistant for non-technical users.
Current Execution Context:
--------------------------
Full Source Code:
{context_code if context_code else "Not provided"}
Target Line {line_number} (Currently executing):
{line_content}
Execution Count: {exec_count}
Call Stack (Most recent call last):
{json.dumps(stack, indent=2) if stack else "Empty"}
Output So Far:
{stdout}
--------------------------
Your Goal:
1. Explain what is happening in the code in simple, everyday language suitable for a beginner or non-technical person.
2. Avoid complex technical jargon. If you must use a term, explain it briefly.
3. Focus on the "logic" and "flow" rather than memory addresses or implementation details.
4. Be concise and efficient. Get straight to the point.
"""
# Build LangChain message list
lc_messages = [SystemMessage(content=system_prompt)]
for msg in messages:
role = msg.get('role')
content = msg.get('content')
if role == 'user':
lc_messages.append(HumanMessage(content=content))
elif role == 'assistant':
lc_messages.append(AIMessage(content=content))
# If no messages yet (initial call), add the default prompt
if not messages:
if error_message:
lc_messages.append(HumanMessage(content=f"Please explain this error: {error_message}"))
else:
lc_messages.append(HumanMessage(content=f"Explain what line {line_number} is doing."))
try:
response = llm.invoke(lc_messages)
return response.content
except Exception as e:
return f"Error calling LLM: {str(e)}"