Ankush Rana commited on
Commit
5f9dbfa
·
1 Parent(s): eac045a

llm in the loop

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. __pycache__/tools.cpython-310.pyc +0 -0
  3. 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["HF_TOKEN"]
13
- BASE_URL = os.environ["BASE_URL"]
14
 
15
- SYSTEM_PROMPT_TEMPLATE = """You are an AI assistant designed to assist users with a hotel booking and information system. Your role is to provide detailed and accurate information about the hotel, including available accommodations, facilities, dining options, and reservation services. You can check room availability, assist with bookings, modify or cancel reservations, and answer general inquiries about the hotel.
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
- base_url=BASE_URL + "/v1",
22
- api_key=HF_TOKEN
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 complation(history, model, system_prompt, tools=None):
31
  messages = [{"role": "system", "content": system_prompt}]
32
  for msg in history:
33
- if type(msg) == dict:
34
  msg = ChatMessage(**msg)
35
- if msg.role == "assistant" and len(msg.metadata):
36
- tools_calls = json.loads(msg.metadata["title"])
 
 
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
- if not tools:
42
- return client.chat.completions.create(
43
- model=model,
44
- messages=messages,
45
- stream=True,
46
- max_tokens=1000,
47
- temperature=0.4,
48
- frequency_penalty=1,
49
- # stop=["<|em_end|>"],
50
- extra_body = {
51
- "repetition_penalty": 1.1,
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 respond(
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.append(
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 completion:
93
- if len(chunk.choices) > 0 and chunk.choices[0].delta.tool_calls and len(chunk.choices[0].delta.tool_calls) > 0 :
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[-1].content = response
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}, tools doesn't exists"
121
- print("arguments:", arguments)
122
- json_arguments = json.loads(arguments)
123
- history.append(
124
- ChatMessage(
125
- role="assistant",
126
- content="",
127
- metadata= {"title": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}
128
- )
129
- )
130
- yield history[-1]
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
- completion = complation(history=history, tools=oitools, model=model, system_prompt=additional_inputs)
142
- result = ""
143
- appended = False
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 propmt", value=SYSTEM_PROMPT_TEMPLATE, lines=3)
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()