Spaces:
Runtime error
Runtime error
File size: 3,375 Bytes
dd4bbec 1352961 dd4bbec 1352961 dd4bbec 1352961 dd4bbec |
1 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import gradio as gr
import json
import os
import numexpr
from groq import Groq
from groq.types.chat.chat_completion_tool_param import ChatCompletionToolParam
MODEL = "llama3-groq-8b-8192-tool-use-preview"
client = Groq(api_key=os.environ["GROQ_API_KEY"])
def evaluate_math_expression(expression: str):
return json.dumps(numexpr.evaluate(expression).tolist())
calculator_tool: ChatCompletionToolParam = {
"type": "function",
"function": {
"name": "evaluate_math_expression",
"description":
"Calculator tool: use this for evaluating numeric expressions with Python. Ensure the expression is valid Python syntax (e.g., use '**' for exponentiation, not '^').",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "The mathematical expression to evaluate. Must be valid Python syntax.",
},
},
"required": ["expression"],
},
},
}
tools = [calculator_tool]
def call_function(tool_call, available_functions):
function_name = tool_call.function.name
if function_name not in available_functions:
return {
"tool_call_id": tool_call.id,
"role": "tool",
"content": f"Function {function_name} does not exist.",
}
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(**function_args)
return {
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": json.dumps(function_response),
}
def get_model_response(messages):
return client.chat.completions.create(
model=MODEL,
messages=messages,
tools=tools,
temperature=0.5,
top_p=0.65,
max_tokens=4096,
)
def respond(message, history, system_message):
conversation = [{"role": "system", "content": system_message}]
for human, assistant in history:
conversation.append({"role": "user", "content": human})
conversation.append({"role": "assistant", "content": assistant})
conversation.append({"role": "user", "content": message})
available_functions = {
"evaluate_math_expression": evaluate_math_expression,
}
while True:
response = get_model_response(conversation)
response_message = response.choices[0].message
conversation.append(response_message)
if not response_message.tool_calls and response_message.content is not None:
return response_message.content
if response_message.tool_calls is not None:
for tool_call in response_message.tool_calls:
function_response = call_function(tool_call, available_functions)
conversation.append(function_response)
demo = gr.ChatInterface(
respond,
additional_inputs=[
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"),
],
title="Groq Tool Use Chat",
description="This chatbot uses the Groq LLM with tool use capabilities, including a calculator function.",
)
if __name__ == "__main__":
demo.launch() |