Spaces:
Sleeping
Sleeping
Ankush Rana
commited on
Commit
·
d240252
1
Parent(s):
89f7fd0
add date in system prompt
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import json
|
|
5 |
from openai import OpenAI
|
6 |
from tools import tools, oitools
|
7 |
from dotenv import load_dotenv
|
|
|
8 |
import os
|
9 |
import re
|
10 |
|
@@ -17,7 +18,8 @@ SYSTEM_PROMPT_TEMPLATE = """You are an AI assistant designed to assist users wit
|
|
17 |
Maintain clarity, conciseness, and relevance in your responses, ensuring a seamless user experience.
|
18 |
Always respond in the same language as the user’s query to preserve their preferred language.
|
19 |
|
20 |
-
If the user asks for any information about it's history, a region, its cities, activities, tourism, or surroundings, please use the function `get_documents`.
|
|
|
21 |
|
22 |
|
23 |
# print(json.dumps(oitools, indent=2))
|
@@ -26,11 +28,15 @@ client = OpenAI(
|
|
26 |
api_key=HF_TOKEN
|
27 |
)
|
28 |
|
|
|
|
|
|
|
|
|
29 |
def clean_json_string(json_str):
|
30 |
return re.sub(r'[ ,}\s]+$', '', json_str) + '}'
|
31 |
|
32 |
-
def completion(history, model, system_prompt, tools=None):
|
33 |
-
messages = [{"role": "system", "content": system_prompt}]
|
34 |
for msg in history:
|
35 |
if isinstance(msg, dict):
|
36 |
msg = ChatMessage(**msg)
|
@@ -43,13 +49,17 @@ def completion(history, model, system_prompt, tools=None):
|
|
43 |
else:
|
44 |
messages.append({"role": msg.role, "content": msg.content})
|
45 |
|
|
|
|
|
46 |
|
|
|
|
|
47 |
request_params = {
|
48 |
"model": model,
|
49 |
"messages": messages,
|
50 |
-
"stream":
|
51 |
"max_tokens": 1000,
|
52 |
-
"temperature": 0.
|
53 |
"frequency_penalty": 1,
|
54 |
"extra_body": {"repetition_penalty": 1.1},
|
55 |
}
|
@@ -70,31 +80,31 @@ def llm_in_loop(history, system_prompt, recursive):
|
|
70 |
name = ""
|
71 |
chat_completion = completion(history=history, tools=oitools, model=model, system_prompt=system_prompt)
|
72 |
appended = False
|
73 |
-
if chat_completion.choices and chat_completion.choices[0].message.tool_calls:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
elif chat_completion.choices[0].message.content:
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
|
99 |
arguments = clean_json_string(arguments) if arguments else "{}"
|
100 |
print(name, arguments)
|
|
|
5 |
from openai import OpenAI
|
6 |
from tools import tools, oitools
|
7 |
from dotenv import load_dotenv
|
8 |
+
from datetime import datetime
|
9 |
import os
|
10 |
import re
|
11 |
|
|
|
18 |
Maintain clarity, conciseness, and relevance in your responses, ensuring a seamless user experience.
|
19 |
Always respond in the same language as the user’s query to preserve their preferred language.
|
20 |
|
21 |
+
If the user asks for any information about it's history, a region, its cities, activities, tourism, or surroundings, please use the function `get_documents`.
|
22 |
+
Today is {date}"""
|
23 |
|
24 |
|
25 |
# print(json.dumps(oitools, indent=2))
|
|
|
28 |
api_key=HF_TOKEN
|
29 |
)
|
30 |
|
31 |
+
def today_date():
|
32 |
+
return datetime.today().strftime('%A, %B %d, %Y, %I:%M %p')
|
33 |
+
|
34 |
+
|
35 |
def clean_json_string(json_str):
|
36 |
return re.sub(r'[ ,}\s]+$', '', json_str) + '}'
|
37 |
|
38 |
+
def completion(history, model, system_prompt: str, tools=None):
|
39 |
+
messages = [{"role": "system", "content": system_prompt.format(date=today_date())}]
|
40 |
for msg in history:
|
41 |
if isinstance(msg, dict):
|
42 |
msg = ChatMessage(**msg)
|
|
|
49 |
else:
|
50 |
messages.append({"role": msg.role, "content": msg.content})
|
51 |
|
52 |
+
for msg in messages:
|
53 |
+
print(msg)
|
54 |
|
55 |
+
print("")
|
56 |
+
print("")
|
57 |
request_params = {
|
58 |
"model": model,
|
59 |
"messages": messages,
|
60 |
+
"stream": True,
|
61 |
"max_tokens": 1000,
|
62 |
+
"temperature": 0.15,
|
63 |
"frequency_penalty": 1,
|
64 |
"extra_body": {"repetition_penalty": 1.1},
|
65 |
}
|
|
|
80 |
name = ""
|
81 |
chat_completion = completion(history=history, tools=oitools, model=model, system_prompt=system_prompt)
|
82 |
appended = False
|
83 |
+
# if chat_completion.choices and chat_completion.choices[0].message.tool_calls:
|
84 |
+
# call = chat_completion.choices[0].message.tool_calls[0]
|
85 |
+
# if hasattr(call.function, "name") and call.function.name:
|
86 |
+
# name = call.function.name
|
87 |
+
# if hasattr(call.function, "arguments") and call.function.arguments:
|
88 |
+
# arguments += call.function.arguments
|
89 |
+
# elif chat_completion.choices[0].message.content:
|
90 |
+
# if not appended:
|
91 |
+
# history.append(ChatMessage(role="assistant", content=""))
|
92 |
+
# appended = True
|
93 |
+
# history[-1].content += chat_completion.choices[0].message.content
|
94 |
+
# yield history[recursive:]
|
95 |
+
for chunk in chat_completion:
|
96 |
+
if chunk.choices and chunk.choices[0].delta.tool_calls:
|
97 |
+
call = chunk.choices[0].delta.tool_calls[0]
|
98 |
+
if hasattr(call.function, "name") and call.function.name:
|
99 |
+
name = call.function.name
|
100 |
+
if hasattr(call.function, "arguments") and call.function.arguments:
|
101 |
+
arguments += call.function.arguments
|
102 |
+
elif chunk.choices[0].delta.content:
|
103 |
+
if not appended:
|
104 |
+
history.append(ChatMessage(role="assistant", content=""))
|
105 |
+
appended = True
|
106 |
+
history[-1].content += chunk.choices[0].delta.content
|
107 |
+
yield history[recursive:]
|
108 |
|
109 |
arguments = clean_json_string(arguments) if arguments else "{}"
|
110 |
print(name, arguments)
|