Spaces:
Runtime error
Runtime error
ricklamers
commited on
Commit
•
1352961
1
Parent(s):
79e055d
feat: add tool use
Browse files- app.py +88 -54
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,63 +1,97 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
|
|
|
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import numexpr
|
5 |
+
from groq import Groq
|
6 |
+
from groq.types.chat.chat_completion_tool_param import ChatCompletionToolParam
|
7 |
+
|
8 |
+
MODEL = "llama3-groq-8b-8192-tool-use-preview"
|
9 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
10 |
+
|
11 |
+
def evaluate_math_expression(expression: str):
|
12 |
+
return json.dumps(numexpr.evaluate(expression).tolist())
|
13 |
+
|
14 |
+
calculator_tool: ChatCompletionToolParam = {
|
15 |
+
"type": "function",
|
16 |
+
"function": {
|
17 |
+
"name": "evaluate_math_expression",
|
18 |
+
"description":
|
19 |
+
"Calculator tool: use this for evaluating numeric expressions with Python. Ensure the expression is valid Python syntax (e.g., use '**' for exponentiation, not '^').",
|
20 |
+
"parameters": {
|
21 |
+
"type": "object",
|
22 |
+
"properties": {
|
23 |
+
"expression": {
|
24 |
+
"type": "string",
|
25 |
+
"description": "The mathematical expression to evaluate. Must be valid Python syntax.",
|
26 |
+
},
|
27 |
+
},
|
28 |
+
"required": ["expression"],
|
29 |
+
},
|
30 |
+
},
|
31 |
+
}
|
32 |
+
|
33 |
+
tools = [calculator_tool]
|
34 |
+
|
35 |
+
def call_function(tool_call, available_functions):
|
36 |
+
function_name = tool_call.function.name
|
37 |
+
if function_name not in available_functions:
|
38 |
+
return {
|
39 |
+
"tool_call_id": tool_call.id,
|
40 |
+
"role": "tool",
|
41 |
+
"content": f"Function {function_name} does not exist.",
|
42 |
+
}
|
43 |
+
function_to_call = available_functions[function_name]
|
44 |
+
function_args = json.loads(tool_call.function.arguments)
|
45 |
+
function_response = function_to_call(**function_args)
|
46 |
+
return {
|
47 |
+
"tool_call_id": tool_call.id,
|
48 |
+
"role": "tool",
|
49 |
+
"name": function_name,
|
50 |
+
"content": json.dumps(function_response),
|
51 |
+
}
|
52 |
+
|
53 |
+
def get_model_response(messages):
|
54 |
+
return client.chat.completions.create(
|
55 |
+
model=MODEL,
|
56 |
+
messages=messages,
|
57 |
+
tools=tools,
|
58 |
+
temperature=0.5,
|
59 |
+
top_p=0.65,
|
60 |
+
max_tokens=4096,
|
61 |
+
)
|
62 |
+
|
63 |
+
def respond(message, history, system_message):
|
64 |
+
conversation = [{"role": "system", "content": system_message}]
|
65 |
+
for human, assistant in history:
|
66 |
+
conversation.append({"role": "user", "content": human})
|
67 |
+
conversation.append({"role": "assistant", "content": assistant})
|
68 |
+
conversation.append({"role": "user", "content": message})
|
69 |
+
|
70 |
+
available_functions = {
|
71 |
+
"evaluate_math_expression": evaluate_math_expression,
|
72 |
+
}
|
73 |
+
|
74 |
+
while True:
|
75 |
+
response = get_model_response(conversation)
|
76 |
+
response_message = response.choices[0].message
|
77 |
+
conversation.append(response_message)
|
78 |
+
|
79 |
+
if not response_message.tool_calls and response_message.content is not None:
|
80 |
+
return response_message.content
|
81 |
+
|
82 |
+
if response_message.tool_calls is not None:
|
83 |
+
for tool_call in response_message.tool_calls:
|
84 |
+
function_response = call_function(tool_call, available_functions)
|
85 |
+
conversation.append(function_response)
|
86 |
+
|
87 |
demo = gr.ChatInterface(
|
88 |
respond,
|
89 |
additional_inputs=[
|
90 |
+
gr.Textbox(value="You are a friendly Chatbot with access to a calculator. Don't mention that we are using functions defined in Python.", label="System message"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
],
|
92 |
+
title="Groq Tool Use Chat",
|
93 |
+
description="This chatbot uses the Groq LLM with tool use capabilities, including a calculator function.",
|
94 |
)
|
95 |
|
|
|
96 |
if __name__ == "__main__":
|
97 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
huggingface_hub==0.22.2
|
|
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
groq
|
3 |
+
numexpr
|