Spaces:
Runtime error
Runtime error
abhilashnl2006
commited on
Commit
•
79a8c15
1
Parent(s):
76ab566
Update app.py
Browse files
app.py
CHANGED
@@ -7,26 +7,20 @@ openai.api_key = "sk-proj-zVyjTUhcNh6MVzZpoHh3T3BlbkFJzsEfIBENz7WlbVIKv5tS"
|
|
7 |
def get_python_help(message, history):
|
8 |
if history is None:
|
9 |
history = []
|
10 |
-
|
11 |
try:
|
12 |
-
# Construct the
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
User: {message}
|
18 |
-
Assistant:"""
|
19 |
-
|
20 |
-
# Get the full conversation history
|
21 |
-
full_history = "\n".join([f"Human: {h[0]}\nAssistant: {h[1]}" for h in history])
|
22 |
|
23 |
-
|
24 |
-
full_prompt = f"{full_history}\n{prompt}"
|
25 |
|
26 |
-
# Generate a response using
|
27 |
-
response = openai.
|
28 |
-
model="gpt-3.5-turbo",
|
29 |
-
|
30 |
max_tokens=500,
|
31 |
n=1,
|
32 |
stop=None,
|
@@ -34,7 +28,8 @@ def get_python_help(message, history):
|
|
34 |
)
|
35 |
|
36 |
# Extract and return the generated message
|
37 |
-
|
|
|
38 |
except Exception as e:
|
39 |
return f"Error: {str(e)}", history
|
40 |
|
|
|
7 |
def get_python_help(message, history):
|
8 |
if history is None:
|
9 |
history = []
|
10 |
+
|
11 |
try:
|
12 |
+
# Construct the messages from the history
|
13 |
+
messages = [{"role": "system", "content": "You are a helpful assistant that teaches Python programming. You always refer to the latest Python documentation when answering questions. If you're not sure about something, you say so and suggest checking the official documentation."}]
|
14 |
+
for h in history:
|
15 |
+
messages.append({"role": "user", "content": h[0]})
|
16 |
+
messages.append({"role": "assistant", "content": h[1]})
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
messages.append({"role": "user", "content": message})
|
|
|
19 |
|
20 |
+
# Generate a response using gpt-3.5-turbo
|
21 |
+
response = openai.ChatCompletion.create(
|
22 |
+
model="gpt-3.5-turbo",
|
23 |
+
messages=messages,
|
24 |
max_tokens=500,
|
25 |
n=1,
|
26 |
stop=None,
|
|
|
28 |
)
|
29 |
|
30 |
# Extract and return the generated message
|
31 |
+
reply = response.choices[0].message['content'].strip()
|
32 |
+
return reply, history + [(message, reply)]
|
33 |
except Exception as e:
|
34 |
return f"Error: {str(e)}", history
|
35 |
|