Spaces:
Running
Running
Commit
·
99e6996
1
Parent(s):
60ed75d
fix: get rid of custom cache
Browse files
app.py
CHANGED
|
@@ -2,22 +2,22 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import numexpr
|
| 5 |
-
import uuid
|
| 6 |
from groq import Groq
|
| 7 |
from groq.types.chat.chat_completion_tool_param import ChatCompletionToolParam
|
| 8 |
|
| 9 |
MODEL = "llama3-groq-8b-8192-tool-use-preview"
|
| 10 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 11 |
|
|
|
|
| 12 |
def evaluate_math_expression(expression: str):
|
| 13 |
return json.dumps(numexpr.evaluate(expression).tolist())
|
| 14 |
|
|
|
|
| 15 |
calculator_tool: ChatCompletionToolParam = {
|
| 16 |
"type": "function",
|
| 17 |
"function": {
|
| 18 |
"name": "evaluate_math_expression",
|
| 19 |
-
"description":
|
| 20 |
-
"Calculator tool: use this for evaluating numeric expressions with Python. Ensure the expression is valid Python syntax (e.g., use '**' for exponentiation, not '^').",
|
| 21 |
"parameters": {
|
| 22 |
"type": "object",
|
| 23 |
"properties": {
|
|
@@ -33,6 +33,7 @@ calculator_tool: ChatCompletionToolParam = {
|
|
| 33 |
|
| 34 |
tools = [calculator_tool]
|
| 35 |
|
|
|
|
| 36 |
def call_function(tool_call, available_functions):
|
| 37 |
function_name = tool_call.function.name
|
| 38 |
if function_name not in available_functions:
|
|
@@ -51,11 +52,35 @@ def call_function(tool_call, available_functions):
|
|
| 51 |
"content": json.dumps(function_response),
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
return client.chat.completions.create(
|
| 57 |
model=MODEL,
|
| 58 |
-
messages=
|
| 59 |
tools=tools,
|
| 60 |
temperature=0.5,
|
| 61 |
top_p=0.65,
|
|
@@ -63,70 +88,107 @@ def get_model_response(messages):
|
|
| 63 |
)
|
| 64 |
except Exception as e:
|
| 65 |
print(f"An error occurred while getting model response: {str(e)}")
|
| 66 |
-
print(
|
| 67 |
return None
|
| 68 |
|
| 69 |
-
conversation_state = {}
|
| 70 |
|
| 71 |
def respond(message, history, system_message):
|
| 72 |
-
|
| 73 |
-
session_id = str(uuid.uuid4())
|
| 74 |
-
history.insert(0, (session_id, "Confirmed."))
|
| 75 |
-
else:
|
| 76 |
-
session_id = history[0][0]
|
| 77 |
-
|
| 78 |
-
if session_id not in conversation_state:
|
| 79 |
-
conversation_state[session_id] = []
|
| 80 |
-
|
| 81 |
-
if len(conversation_state[session_id]) == 0:
|
| 82 |
-
conversation_state[session_id].append({"role": "system", "content": system_message})
|
| 83 |
-
|
| 84 |
-
conversation_state[session_id].append({"role": "user", "content": message})
|
| 85 |
|
| 86 |
available_functions = {
|
| 87 |
"evaluate_math_expression": evaluate_math_expression,
|
| 88 |
}
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
while True:
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
if not response_message.tool_calls and response_message.content is not None:
|
| 97 |
break
|
| 98 |
|
| 99 |
if response_message.tool_calls is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
for tool_call in response_message.tool_calls:
|
| 101 |
-
function_call = {
|
| 102 |
-
"name": tool_call.function.name,
|
| 103 |
-
"arguments": json.loads(tool_call.function.arguments)
|
| 104 |
-
}
|
| 105 |
-
function_calls.append(function_call)
|
| 106 |
function_response = call_function(tool_call, available_functions)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
call = function_calls[i]
|
| 116 |
-
result = function_calls[i + 1] if i + 1 < len(function_calls) else None
|
| 117 |
-
function_calls_md += f"**Tool call:**\n```json\n{json.dumps({'name': call['name'], 'arguments': call['arguments'], 'result': result['result'] if result else None}, indent=2)}\n```\n"
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
|
| 122 |
demo = gr.ChatInterface(
|
| 123 |
respond,
|
| 124 |
additional_inputs=[
|
| 125 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 126 |
],
|
|
|
|
| 127 |
title="Groq Tool Use Chat",
|
| 128 |
description="This chatbot uses the `llama3-groq-8b-8192-tool-use-preview` LLM with tool use capabilities, including a calculator function.",
|
| 129 |
)
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
-
demo.launch()
|
|
|
|
| 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 |
+
|
| 12 |
def evaluate_math_expression(expression: str):
|
| 13 |
return json.dumps(numexpr.evaluate(expression).tolist())
|
| 14 |
|
| 15 |
+
|
| 16 |
calculator_tool: ChatCompletionToolParam = {
|
| 17 |
"type": "function",
|
| 18 |
"function": {
|
| 19 |
"name": "evaluate_math_expression",
|
| 20 |
+
"description": "Calculator tool: use this for evaluating numeric expressions with Python. Ensure the expression is valid Python syntax (e.g., use '**' for exponentiation, not '^').",
|
|
|
|
| 21 |
"parameters": {
|
| 22 |
"type": "object",
|
| 23 |
"properties": {
|
|
|
|
| 33 |
|
| 34 |
tools = [calculator_tool]
|
| 35 |
|
| 36 |
+
|
| 37 |
def call_function(tool_call, available_functions):
|
| 38 |
function_name = tool_call.function.name
|
| 39 |
if function_name not in available_functions:
|
|
|
|
| 52 |
"content": json.dumps(function_response),
|
| 53 |
}
|
| 54 |
|
| 55 |
+
|
| 56 |
+
def get_model_response(messages, inner_messages, message, system_message):
|
| 57 |
+
messages_for_model = []
|
| 58 |
+
for msg in messages:
|
| 59 |
+
native_messages = msg.get("metadata", {}).get("native_messages", [msg])
|
| 60 |
+
if isinstance(native_messages, list):
|
| 61 |
+
messages_for_model.extend(native_messages)
|
| 62 |
+
else:
|
| 63 |
+
messages_for_model.append(native_messages)
|
| 64 |
+
|
| 65 |
+
messages_for_model.insert(
|
| 66 |
+
0,
|
| 67 |
+
{
|
| 68 |
+
"role": "system",
|
| 69 |
+
"content": system_message,
|
| 70 |
+
},
|
| 71 |
+
)
|
| 72 |
+
messages_for_model.append(
|
| 73 |
+
{
|
| 74 |
+
"role": "user",
|
| 75 |
+
"content": message,
|
| 76 |
+
}
|
| 77 |
+
)
|
| 78 |
+
messages_for_model.extend(inner_messages)
|
| 79 |
+
|
| 80 |
try:
|
| 81 |
return client.chat.completions.create(
|
| 82 |
model=MODEL,
|
| 83 |
+
messages=messages_for_model,
|
| 84 |
tools=tools,
|
| 85 |
temperature=0.5,
|
| 86 |
top_p=0.65,
|
|
|
|
| 88 |
)
|
| 89 |
except Exception as e:
|
| 90 |
print(f"An error occurred while getting model response: {str(e)}")
|
| 91 |
+
print(messages_for_model)
|
| 92 |
return None
|
| 93 |
|
|
|
|
| 94 |
|
| 95 |
def respond(message, history, system_message):
|
| 96 |
+
inner_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
available_functions = {
|
| 99 |
"evaluate_math_expression": evaluate_math_expression,
|
| 100 |
}
|
| 101 |
|
| 102 |
+
assistant_content = ""
|
| 103 |
+
assistant_native_message_list = []
|
| 104 |
+
|
| 105 |
while True:
|
| 106 |
+
response_message = (
|
| 107 |
+
get_model_response(history, inner_history, message, system_message)
|
| 108 |
+
.choices[0]
|
| 109 |
+
.message
|
| 110 |
+
)
|
| 111 |
|
| 112 |
if not response_message.tool_calls and response_message.content is not None:
|
| 113 |
break
|
| 114 |
|
| 115 |
if response_message.tool_calls is not None:
|
| 116 |
+
assistant_native_message_list.append(response_message)
|
| 117 |
+
inner_history.append(response_message)
|
| 118 |
+
|
| 119 |
+
assistant_content += (
|
| 120 |
+
"```json\n"
|
| 121 |
+
+ json.dumps(
|
| 122 |
+
[
|
| 123 |
+
tool_call.model_dump()
|
| 124 |
+
for tool_call in response_message.tool_calls
|
| 125 |
+
],
|
| 126 |
+
indent=2,
|
| 127 |
+
)
|
| 128 |
+
+ "\n```\n"
|
| 129 |
+
)
|
| 130 |
+
assistant_message = {
|
| 131 |
+
"role": "assistant",
|
| 132 |
+
"content": assistant_content,
|
| 133 |
+
"metadata": {"native_messages": assistant_native_message_list},
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
yield assistant_message
|
| 137 |
+
|
| 138 |
for tool_call in response_message.tool_calls:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
function_response = call_function(tool_call, available_functions)
|
| 140 |
+
assistant_content += (
|
| 141 |
+
"```json\n"
|
| 142 |
+
+ json.dumps(
|
| 143 |
+
{
|
| 144 |
+
"name": tool_call.function.name,
|
| 145 |
+
"arguments": json.loads(tool_call.function.arguments),
|
| 146 |
+
"response": json.loads(function_response["content"]),
|
| 147 |
+
},
|
| 148 |
+
indent=2,
|
| 149 |
+
)
|
| 150 |
+
+ "\n```\n"
|
| 151 |
+
)
|
| 152 |
+
native_tool_message = {
|
| 153 |
+
"tool_call_id": tool_call.id,
|
| 154 |
+
"role": "tool",
|
| 155 |
+
"content": function_response["content"],
|
| 156 |
+
}
|
| 157 |
+
assistant_native_message_list.append(
|
| 158 |
+
native_tool_message
|
| 159 |
+
)
|
| 160 |
+
tool_message = {
|
| 161 |
+
"role": "assistant",
|
| 162 |
+
"content": assistant_content,
|
| 163 |
+
"metadata": {"native_messages": assistant_native_message_list},
|
| 164 |
+
}
|
| 165 |
+
yield tool_message
|
| 166 |
+
inner_history.append(native_tool_message)
|
| 167 |
|
| 168 |
+
assistant_content += response_message.content
|
| 169 |
+
assistant_native_message_list.append(response_message)
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
final_message = {
|
| 172 |
+
"role": "assistant",
|
| 173 |
+
"content": assistant_content,
|
| 174 |
+
"metadata": {"native_messages": assistant_native_message_list},
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
yield final_message
|
| 178 |
|
| 179 |
|
| 180 |
demo = gr.ChatInterface(
|
| 181 |
respond,
|
| 182 |
additional_inputs=[
|
| 183 |
+
gr.Textbox(
|
| 184 |
+
value="You are a friendly Chatbot with access to a calculator. Don't mention that we are using functions defined in Python.",
|
| 185 |
+
label="System message",
|
| 186 |
+
),
|
| 187 |
],
|
| 188 |
+
type="messages",
|
| 189 |
title="Groq Tool Use Chat",
|
| 190 |
description="This chatbot uses the `llama3-groq-8b-8192-tool-use-preview` LLM with tool use capabilities, including a calculator function.",
|
| 191 |
)
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|
| 194 |
+
demo.launch()
|