Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,56 +7,31 @@ from mistralai.models.chat_completion import ChatMessage
|
|
7 |
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
client = MistralClient(api_key= os.getenv('mistral_api_key'))
|
9 |
|
10 |
-
|
11 |
# Function to get chat response
|
12 |
def get_mistral_response(user_input, history: list[tuple[str, str]], context):
|
13 |
messages = [ChatMessage(role='user', content='you are helpful assistant answering question based on given context. provide a highly consie and precise answer, along with a citation from original context in the format of JSON.'),
|
14 |
ChatMessage(role='assistant', content='I understand.')] # system prompt has been baked into fine-tuned model.
|
15 |
|
16 |
context_with_history = context # placeholder
|
17 |
-
|
18 |
model='ft:open-mistral-7b:1c04df3c:20240629:7010a3c8',
|
19 |
messages=[ChatMessage(role='user', content=f'''CONTEXT:{context_with_history} QUESTION: {user_input}''')]
|
20 |
-
)
|
21 |
-
response = chat_response.choices[0].message.content
|
22 |
-
return response
|
23 |
-
|
24 |
-
|
25 |
-
def get_zephyr_respond(
|
26 |
-
message,
|
27 |
-
history: list[tuple[str, str]],
|
28 |
-
system_message,
|
29 |
-
max_tokens,
|
30 |
-
temperature,
|
31 |
-
top_p):
|
32 |
-
messages = [{"role": "system", "content": system_message}]
|
33 |
-
|
34 |
-
for val in history:
|
35 |
-
if val[0]:
|
36 |
-
messages.append({"role": "user", "content": val[0]})
|
37 |
-
if val[1]:
|
38 |
-
messages.append({"role": "assistant", "content": val[1]})
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
for message in client.chat_completion(
|
45 |
-
messages,
|
46 |
-
max_tokens=max_tokens,
|
47 |
-
stream=True,
|
48 |
-
temperature=temperature,
|
49 |
-
top_p=top_p,
|
50 |
-
):
|
51 |
-
token = message.choices[0].delta.content
|
52 |
|
53 |
-
response += token
|
54 |
-
yield response
|
55 |
|
56 |
|
57 |
demo = gr.ChatInterface(
|
58 |
get_mistral_response,
|
59 |
-
title='no-nonsense
|
60 |
description="This bot answers your question with grounded citation. Copy paste your contextual information in the box below.",
|
61 |
additional_inputs=[
|
62 |
gr.Textbox(value="", label="Answer will be based on the context input here", lines=10),
|
|
|
7 |
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
client = MistralClient(api_key= os.getenv('mistral_api_key'))
|
9 |
|
|
|
10 |
# Function to get chat response
|
11 |
def get_mistral_response(user_input, history: list[tuple[str, str]], context):
|
12 |
messages = [ChatMessage(role='user', content='you are helpful assistant answering question based on given context. provide a highly consie and precise answer, along with a citation from original context in the format of JSON.'),
|
13 |
ChatMessage(role='assistant', content='I understand.')] # system prompt has been baked into fine-tuned model.
|
14 |
|
15 |
context_with_history = context # placeholder
|
16 |
+
response_after = client.chat(
|
17 |
model='ft:open-mistral-7b:1c04df3c:20240629:7010a3c8',
|
18 |
messages=[ChatMessage(role='user', content=f'''CONTEXT:{context_with_history} QUESTION: {user_input}''')]
|
19 |
+
).choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
response_before = client.chat(
|
22 |
+
model='open-mistral-7b',
|
23 |
+
messages=[ChatMessage(role='user', content=f'''CONTEXT:{context_with_history} QUESTION: {user_input}''')]
|
24 |
+
).choices[0].message.content
|
25 |
+
|
26 |
+
response = f"**Mistral before fine-tune**: <br> {response_before} <br> **Mistral after fine-tune**:<br> {response_after} <br>"
|
27 |
+
return response
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
|
|
|
|
30 |
|
31 |
|
32 |
demo = gr.ChatInterface(
|
33 |
get_mistral_response,
|
34 |
+
title='no-nonsense QA bot',
|
35 |
description="This bot answers your question with grounded citation. Copy paste your contextual information in the box below.",
|
36 |
additional_inputs=[
|
37 |
gr.Textbox(value="", label="Answer will be based on the context input here", lines=10),
|