Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,26 +25,31 @@ def generate_context(text):
|
|
| 25 |
contexts = contexts + [x['metadata']['text'] for x in result['matches']]
|
| 26 |
return contexts
|
| 27 |
|
| 28 |
-
def invoke_openai(prompt):
|
| 29 |
sys_prompt = "You are a helpful assistant that always answers questions."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# query text-davinci-003
|
| 31 |
res = client.chat.completions.create(
|
| 32 |
model='gpt-3.5-turbo',
|
| 33 |
-
messages=
|
| 34 |
-
{"role": "system", "content": sys_prompt},
|
| 35 |
-
{"role": "system", "content": "Use only the context to answer the question"},
|
| 36 |
-
{"role": "user", "content": prompt}
|
| 37 |
-
],
|
| 38 |
temperature=0
|
| 39 |
)
|
| 40 |
return res.choices[0].message.content
|
| 41 |
|
| 42 |
def build_prompt(message,history):
|
| 43 |
context=generate_context(message)
|
| 44 |
-
messages=[]
|
| 45 |
prompt=f'Context - {context}\nBased on the above context, answer this question - {message}'
|
| 46 |
print(prompt)
|
| 47 |
-
return invoke_openai(prompt)
|
| 48 |
|
| 49 |
|
| 50 |
iface = gr.ChatInterface(build_prompt, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="Basic chat on NLP Data", examples = ["Which training method should I use for sentence transformers when I only have pairs of related sentences?"], theme="soft", cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear",)
|
|
|
|
| 25 |
contexts = contexts + [x['metadata']['text'] for x in result['matches']]
|
| 26 |
return contexts
|
| 27 |
|
| 28 |
+
def invoke_openai(prompt,history):
|
| 29 |
sys_prompt = "You are a helpful assistant that always answers questions."
|
| 30 |
+
new_messages=[
|
| 31 |
+
{"role": "system", "content": sys_prompt},
|
| 32 |
+
{"role": "system", "content": "Use only the context to answer the question"},
|
| 33 |
+
]
|
| 34 |
+
for conv in history:
|
| 35 |
+
user = conv[0]
|
| 36 |
+
new_messages.append({"role": "user", "content":user })
|
| 37 |
+
assistant = conv[1]
|
| 38 |
+
new_messages.append({"role": "assistant", "content":assistant})
|
| 39 |
+
new_messages.append({"role": "user", "content": prompt})
|
| 40 |
# query text-davinci-003
|
| 41 |
res = client.chat.completions.create(
|
| 42 |
model='gpt-3.5-turbo',
|
| 43 |
+
messages=new_messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
temperature=0
|
| 45 |
)
|
| 46 |
return res.choices[0].message.content
|
| 47 |
|
| 48 |
def build_prompt(message,history):
|
| 49 |
context=generate_context(message)
|
|
|
|
| 50 |
prompt=f'Context - {context}\nBased on the above context, answer this question - {message}'
|
| 51 |
print(prompt)
|
| 52 |
+
return invoke_openai(prompt,history)
|
| 53 |
|
| 54 |
|
| 55 |
iface = gr.ChatInterface(build_prompt, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="Basic chat on NLP Data", examples = ["Which training method should I use for sentence transformers when I only have pairs of related sentences?"], theme="soft", cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear",)
|