Ankush Rana commited on
Commit
611fcdd
·
1 Parent(s): 216f293
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -39,7 +39,13 @@ def today_date():
39
 
40
 
41
  def clean_json_string(json_str):
42
- return re.sub(r'[ ,}\s]+$', '', json_str) + '}'
 
 
 
 
 
 
43
 
44
  def completion(history, model, system_prompt: str, tools=None):
45
  messages = [{"role": "system", "content": system_prompt.format(date=today_date())}]
@@ -107,14 +113,15 @@ def llm_in_loop(history, system_prompt, recursive):
107
  history[-1].content += chunk.choices[0].delta.content
108
  yield history[recursive:]
109
 
110
- arguments = clean_json_string(arguments) if arguments else "{}"
111
- print(name, arguments)
112
- arguments = json.loads(arguments)
113
- print(name, arguments)
114
- print("====================")
115
- if appended:
116
- recursive -= 1
117
  if name:
 
 
 
 
 
 
118
  result = f"💥 Error using tool {name}, tool doesn't exist" if name not in tools else str(tools[name].invoke(input=arguments))
119
  result = json.dumps({name: result}, ensure_ascii=False)
120
  # msg = ChatMessage(
@@ -123,7 +130,13 @@ def llm_in_loop(history, system_prompt, recursive):
123
  # metadata= {"title": f"🛠️ Using tool '{name}', arguments: {json.dumps(json_arguments, ensure_ascii=False)}"},
124
  # options=[{"label":"tool_calls", "value": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}]
125
  # )
126
- history.append(ChatMessage(role="assistant", content=result, metadata={"title": json.dumps([{"id": "call_id", "function": {"arguments": json.dumps(arguments, ensure_ascii=False), "name": name}, "type": "function"}], ensure_ascii=False)}))
 
 
 
 
 
 
127
  yield history[recursive:]
128
  yield from llm_in_loop(history, system_prompt, recursive - 1)
129
 
 
39
 
40
 
41
  def clean_json_string(json_str):
42
+ try:
43
+ data = json.loads(json_str)
44
+ if type(data) == list:
45
+ return json.dumps(data[0])
46
+ return json_str
47
+ except:
48
+ return re.sub(r'[ ,}\s]+$', '', json_str) + '}'
49
 
50
  def completion(history, model, system_prompt: str, tools=None):
51
  messages = [{"role": "system", "content": system_prompt.format(date=today_date())}]
 
113
  history[-1].content += chunk.choices[0].delta.content
114
  yield history[recursive:]
115
 
116
+
117
+
 
 
 
 
 
118
  if name:
119
+ print("------------------------")
120
+ print(name, arguments)
121
+ arguments = clean_json_string(arguments) if arguments else "{}"
122
+ print(name, arguments)
123
+ print("====================")
124
+ arguments = json.loads(arguments)
125
  result = f"💥 Error using tool {name}, tool doesn't exist" if name not in tools else str(tools[name].invoke(input=arguments))
126
  result = json.dumps({name: result}, ensure_ascii=False)
127
  # msg = ChatMessage(
 
130
  # metadata= {"title": f"🛠️ Using tool '{name}', arguments: {json.dumps(json_arguments, ensure_ascii=False)}"},
131
  # options=[{"label":"tool_calls", "value": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}]
132
  # )
133
+ msg = ChatMessage(role="assistant", content=result, metadata={"title": json.dumps([{"id": "call_id", "function": {"arguments": json.dumps(arguments, ensure_ascii=False), "name": name}, "type": "function"}], ensure_ascii=False)})
134
+ if appended:
135
+ print("Text with function", history[-1].content)
136
+ msg.content = history[-1].content + "\n" + msg.content
137
+ history[-1] = msg
138
+ else:
139
+ history.append(msg)
140
  yield history[recursive:]
141
  yield from llm_in_loop(history, system_prompt, recursive - 1)
142