Spaces:
Running
Running
Ankush Rana
commited on
Commit
·
5f9dbfa
1
Parent(s):
eac045a
llm in the loop
Browse files- .gitignore +2 -0
- __pycache__/tools.cpython-310.pyc +0 -0
- app.py +57 -121
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
.env
|
__pycache__/tools.cpython-310.pyc
ADDED
|
Binary file (12.7 kB). View file
|
|
|
app.py
CHANGED
|
@@ -9,158 +9,94 @@ import os
|
|
| 9 |
import re
|
| 10 |
|
| 11 |
load_dotenv(".env")
|
| 12 |
-
HF_TOKEN = os.environ
|
| 13 |
-
BASE_URL = os.environ
|
| 14 |
|
| 15 |
-
SYSTEM_PROMPT_TEMPLATE = """You are an AI assistant designed to assist users with a hotel booking and information system
|
| 16 |
-
|
| 17 |
-
Maintain clarity, conciseness, and relevance in your responses, ensuring a seamless user experience. Always respond in the same **language as the user’s query** to preserve their preferred language.
|
| 18 |
-
"""
|
| 19 |
|
| 20 |
client = OpenAI(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
def clean_json_string(json_str):
|
| 27 |
-
# Strip spaces and '}' from the end, then add back a single '}'
|
| 28 |
return re.sub(r'[ ,}\s]+$', '', json_str) + '}'
|
| 29 |
|
| 30 |
-
def
|
| 31 |
messages = [{"role": "system", "content": system_prompt}]
|
| 32 |
for msg in history:
|
| 33 |
-
if
|
| 34 |
msg = ChatMessage(**msg)
|
| 35 |
-
if msg.role == "assistant" and
|
| 36 |
-
tools_calls = json.loads(msg.metadata
|
|
|
|
|
|
|
| 37 |
messages.append({"role": "assistant", "tool_calls": tools_calls})
|
| 38 |
messages.append({"role": "tool", "content": msg.content})
|
| 39 |
else:
|
| 40 |
messages.append({"role": msg.role, "content": msg.content})
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return client.chat.completions.create(
|
| 55 |
-
model=model,
|
| 56 |
-
messages=messages,
|
| 57 |
-
stream=True,
|
| 58 |
-
max_tokens=1000,
|
| 59 |
-
temperature=0.4,
|
| 60 |
-
tool_choice="auto",
|
| 61 |
-
tools=tools,
|
| 62 |
-
frequency_penalty=1,
|
| 63 |
-
# stop=["<|em_end|>"],
|
| 64 |
-
extra_body = {
|
| 65 |
-
"repetition_penalty": 1.1,
|
| 66 |
-
}
|
| 67 |
-
)
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
message:any,
|
| 71 |
-
history:any,
|
| 72 |
-
additional_inputs,
|
| 73 |
-
):
|
| 74 |
try:
|
| 75 |
models = client.models.list()
|
| 76 |
-
model = models.data[0].id
|
| 77 |
except Exception as err:
|
| 78 |
gr.Warning("The model is initializing. Please wait; this may take 5 to 10 minutes ⏳.", duration=20)
|
| 79 |
raise err
|
| 80 |
-
|
| 81 |
-
response = ""
|
| 82 |
arguments = ""
|
| 83 |
name = ""
|
| 84 |
-
history
|
| 85 |
-
ChatMessage(
|
| 86 |
-
role="user",
|
| 87 |
-
content=message,
|
| 88 |
-
)
|
| 89 |
-
)
|
| 90 |
-
completion = complation(history=history, tools=oitools, model=model, system_prompt=additional_inputs)
|
| 91 |
appended = False
|
| 92 |
-
for chunk in
|
| 93 |
-
if
|
| 94 |
call = chunk.choices[0].delta.tool_calls[0]
|
| 95 |
-
if call.function.name:
|
| 96 |
-
name=call.function.name
|
| 97 |
-
if call.function.arguments:
|
| 98 |
arguments += call.function.arguments
|
| 99 |
-
|
| 100 |
elif chunk.choices[0].delta.content:
|
| 101 |
-
response += chunk.choices[0].delta.content
|
| 102 |
if not appended:
|
| 103 |
-
history.append(
|
| 104 |
-
ChatMessage(
|
| 105 |
-
role="assistant",
|
| 106 |
-
content="",
|
| 107 |
-
)
|
| 108 |
-
)
|
| 109 |
appended = True
|
| 110 |
-
|
| 111 |
-
history[
|
| 112 |
-
yield history[-1]
|
| 113 |
-
|
| 114 |
-
if not arguments:
|
| 115 |
-
arguments = "{}"
|
| 116 |
-
else:
|
| 117 |
-
arguments = clean_json_string(arguments)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
| 119 |
if name:
|
| 120 |
-
result = f"💥 Error using tool {name},
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
yield history
|
| 131 |
-
if tools.get(name):
|
| 132 |
-
result = str(tools[name].invoke(input=json_arguments))
|
| 133 |
-
result = json.dumps({name: result}, ensure_ascii=False)
|
| 134 |
-
history[-1] = ChatMessage(
|
| 135 |
-
role="assistant",
|
| 136 |
-
content=result,
|
| 137 |
-
metadata= {"title": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}
|
| 138 |
-
)
|
| 139 |
-
yield history[-1]
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
for chunk in completion:
|
| 145 |
-
print(chunk)
|
| 146 |
-
if chunk.choices[0].delta.content:
|
| 147 |
-
result += chunk.choices[0].delta.content
|
| 148 |
-
if not appended:
|
| 149 |
-
history.append(
|
| 150 |
-
ChatMessage(
|
| 151 |
-
role="assistant",
|
| 152 |
-
content="",
|
| 153 |
-
)
|
| 154 |
-
)
|
| 155 |
-
appended = True
|
| 156 |
-
|
| 157 |
-
history[-1].content = result
|
| 158 |
-
yield history[-2:]
|
| 159 |
|
| 160 |
-
"""
|
| 161 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 162 |
-
"""
|
| 163 |
if __name__ == "__main__":
|
| 164 |
-
system_prompt = gr.Textbox(label="System
|
| 165 |
demo = gr.ChatInterface(respond, type="messages", additional_inputs=[system_prompt])
|
| 166 |
-
demo.launch()
|
|
|
|
| 9 |
import re
|
| 10 |
|
| 11 |
load_dotenv(".env")
|
| 12 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 13 |
+
BASE_URL = os.environ.get("BASE_URL")
|
| 14 |
|
| 15 |
+
SYSTEM_PROMPT_TEMPLATE = """You are an AI assistant designed to assist users with a hotel booking and information system..."""
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
client = OpenAI(
|
| 18 |
+
base_url=f"{BASE_URL}/v1",
|
| 19 |
+
api_key=HF_TOKEN
|
| 20 |
+
)
|
|
|
|
| 21 |
|
| 22 |
def clean_json_string(json_str):
|
|
|
|
| 23 |
return re.sub(r'[ ,}\s]+$', '', json_str) + '}'
|
| 24 |
|
| 25 |
+
def completion(history, model, system_prompt, tools=None):
|
| 26 |
messages = [{"role": "system", "content": system_prompt}]
|
| 27 |
for msg in history:
|
| 28 |
+
if isinstance(msg, dict):
|
| 29 |
msg = ChatMessage(**msg)
|
| 30 |
+
if msg.role == "assistant" and hasattr(msg, "metadata") and msg.metadata:
|
| 31 |
+
tools_calls = json.loads(msg.metadata.get("title", "[]"))
|
| 32 |
+
# for tool_calls in tools_calls:
|
| 33 |
+
# tool_calls["function"]["arguments"] = json.loads(tool_calls["function"]["arguments"])
|
| 34 |
messages.append({"role": "assistant", "tool_calls": tools_calls})
|
| 35 |
messages.append({"role": "tool", "content": msg.content})
|
| 36 |
else:
|
| 37 |
messages.append({"role": msg.role, "content": msg.content})
|
| 38 |
+
|
| 39 |
+
request_params = {
|
| 40 |
+
"model": model,
|
| 41 |
+
"messages": messages,
|
| 42 |
+
"stream": True,
|
| 43 |
+
"max_tokens": 1000,
|
| 44 |
+
"temperature": 0.4,
|
| 45 |
+
"frequency_penalty": 1,
|
| 46 |
+
"extra_body": {"repetition_penalty": 1.1},
|
| 47 |
+
}
|
| 48 |
+
if tools:
|
| 49 |
+
request_params.update({"tool_choice": "auto", "tools": tools})
|
| 50 |
+
|
| 51 |
+
return client.chat.completions.create(**request_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
def llm_in_loop(history, system_prompt, recursive):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
models = client.models.list()
|
| 56 |
+
model = models.data[0].id if models.data else "gpt-3.5-turbo"
|
| 57 |
except Exception as err:
|
| 58 |
gr.Warning("The model is initializing. Please wait; this may take 5 to 10 minutes ⏳.", duration=20)
|
| 59 |
raise err
|
| 60 |
+
|
|
|
|
| 61 |
arguments = ""
|
| 62 |
name = ""
|
| 63 |
+
chat_completion = completion(history=history, tools=oitools, model=model, system_prompt=system_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
appended = False
|
| 65 |
+
for chunk in chat_completion:
|
| 66 |
+
if chunk.choices and chunk.choices[0].delta.tool_calls:
|
| 67 |
call = chunk.choices[0].delta.tool_calls[0]
|
| 68 |
+
if hasattr(call.function, "name") and call.function.name:
|
| 69 |
+
name = call.function.name
|
| 70 |
+
if hasattr(call.function, "arguments") and call.function.arguments:
|
| 71 |
arguments += call.function.arguments
|
|
|
|
| 72 |
elif chunk.choices[0].delta.content:
|
|
|
|
| 73 |
if not appended:
|
| 74 |
+
history.append(ChatMessage(role="assistant", content=""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
appended = True
|
| 76 |
+
history[-1].content += chunk.choices[0].delta.content
|
| 77 |
+
yield history[recursive:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
arguments = json.loads(clean_json_string(arguments) if arguments else "{}")
|
| 80 |
+
if appended:
|
| 81 |
+
recursive -= 1
|
| 82 |
if name:
|
| 83 |
+
result = f"💥 Error using tool {name}, tool doesn't exist" if name not in tools else str(tools[name].invoke(input=arguments))
|
| 84 |
+
result = json.dumps({name: result}, ensure_ascii=False)
|
| 85 |
+
# msg = ChatMessage(
|
| 86 |
+
# role="assistant",
|
| 87 |
+
# content="",
|
| 88 |
+
# metadata= {"title": f"🛠️ Using tool '{name}', arguments: {json.dumps(json_arguments, ensure_ascii=False)}"},
|
| 89 |
+
# options=[{"label":"tool_calls", "value": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}]
|
| 90 |
+
# )
|
| 91 |
+
history.append(ChatMessage(role="assistant", content=result, metadata={"title": json.dumps([{"id": "call_id", "function": {"arguments": json.dumps(arguments), "name": name}, "type": "function"}])}))
|
| 92 |
+
yield history[recursive:]
|
| 93 |
+
yield from llm_in_loop(history, system_prompt, recursive - 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
def respond(message, history, additional_inputs):
|
| 96 |
+
history.append(ChatMessage(role="user", content=message))
|
| 97 |
+
yield from llm_in_loop(history, additional_inputs, -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
if __name__ == "__main__":
|
| 100 |
+
system_prompt = gr.Textbox(label="System prompt", value=SYSTEM_PROMPT_TEMPLATE, lines=3)
|
| 101 |
demo = gr.ChatInterface(respond, type="messages", additional_inputs=[system_prompt])
|
| 102 |
+
demo.launch()
|