File size: 4,750 Bytes
dd4bbec
1352961
 
 
60ed75d
1352961
 
 
6aa31f2
1352961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a939268
 
 
 
 
 
 
 
 
 
 
 
 
1352961
60ed75d
4d5c750
1352961
60ed75d
 
 
 
 
6403e58
60ed75d
 
 
 
 
 
 
1352961
 
 
 
 
182dc7c
1352961
60ed75d
1352961
60ed75d
1352961
 
182dc7c
1352961
 
 
182dc7c
 
 
 
 
1352961
60ed75d
182dc7c
 
 
 
 
 
 
 
 
 
 
 
1352961
60ed75d
dd4bbec
 
 
1352961
dd4bbec
1352961
6aa31f2
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import gradio as gr
import json
import os
import numexpr
import uuid
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):
    try:
        return client.chat.completions.create(
            model=MODEL,
            messages=messages,
            tools=tools,
            temperature=0.5,
            top_p=0.65,
            max_tokens=4096,
        )
    except Exception as e:
        print(f"An error occurred while getting model response: {str(e)}")
        print(messages)
        return None

conversation_state = {}

def respond(message, history, system_message):
    if not history or not isinstance(history[0][0], str):
        session_id = str(uuid.uuid4())
        history.insert(0, (session_id, "Confirmed."))
    else:
        session_id = history[0][0]

    if session_id not in conversation_state:
        conversation_state[session_id] = []

    if len(conversation_state[session_id]) == 0:
        conversation_state[session_id].append({"role": "system", "content": system_message})

    conversation_state[session_id].append({"role": "user", "content": message})

    available_functions = {
        "evaluate_math_expression": evaluate_math_expression,
    }

    function_calls = []
    while True:
        response = get_model_response(conversation_state[session_id])
        response_message = response.choices[0].message
        conversation_state[session_id].append(response_message)

        if not response_message.tool_calls and response_message.content is not None:
            break

        if response_message.tool_calls is not None:
            for tool_call in response_message.tool_calls:
                function_call = {
                    "name": tool_call.function.name,
                    "arguments": json.loads(tool_call.function.arguments)
                }
                function_calls.append(function_call)
                function_response = call_function(tool_call, available_functions)
                conversation_state[session_id].append(function_response)
                function_calls.append({
                    "name": function_response["name"],
                    "result": json.loads(function_response["content"])
                })

    function_calls_md = "\n\n"
    for i in range(0, len(function_calls), 2):
        call = function_calls[i]
        result = function_calls[i + 1] if i + 1 < len(function_calls) else None
        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"

    return response_message.content + function_calls_md


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 `llama3-groq-8b-8192-tool-use-preview` LLM with tool use capabilities, including a calculator function.",
)

if __name__ == "__main__":
    demo.launch()