File size: 3,549 Bytes
34917ad
3945efd
34917ad
6b3240b
 
 
 
3945efd
 
 
 
 
6b3240b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34917ad
 
6b3240b
 
3945efd
 
 
 
 
 
 
 
 
 
6b3240b
3945efd
6b3240b
 
 
 
 
 
34917ad
6b3240b
34917ad
6b3240b
 
34917ad
6b3240b
 
34917ad
3945efd
 
 
6b3240b
 
 
 
 
34917ad
6b3240b
 
 
3945efd
6b3240b
3945efd
6b3240b
 
 
 
 
34917ad
6b3240b
3945efd
 
 
6b3240b
 
 
 
3945efd
34917ad
3945efd
 
 
6b3240b
3945efd
34917ad
3945efd
 
 
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
import json
import os
import gradio as gr
from huggingface_hub import InferenceClient

# Function to call the Llama 3.1 8B model through Hugging Face API
def call_llama_model(user_query):
    # Initialize the inference client with access token
    client = InferenceClient(
        "meta-llama/Meta-Llama-3.1-8B-Instruct",
        token=os.environ.get("HF_TOKEN")
    )
    
    # Define the addition function schema
    function_schema = {
        "name": "add_numbers",
        "description": "Add two numbers together",
        "parameters": {
            "type": "object",
            "properties": {
                "num1": {
                    "type": "number",
                    "description": "First number to add"
                },
                "num2": {
                    "type": "number",
                    "description": "Second number to add"
                }
            },
            "required": ["num1", "num2"]
        }
    }
    
    # Create the system prompt with function definition
    system_prompt = f"""You have access to the following function:
{json.dumps(function_schema, indent=2)}

When given a query about adding numbers, you must:
1. Extract the two numbers from the query
2. Call the add_numbers function with these numbers
3. Respond ONLY with a valid JSON object in this exact format:
{{"function_call": {{"name": "add_numbers", "parameters": {{"num1": [first number], "num2": [second number]}}}}}}

DO NOT include any explanation, just the JSON.
"""
    
    # Call the model with the appropriate format for Llama 3.1
    response = client.text_generation(
        prompt=f"<|system|>\n{system_prompt}\n<|user|>\n{user_query}\n<|assistant|>",
        max_new_tokens=256,
        temperature=0.1,
        return_full_text=False
    )
    
    return response

# Function to parse the model response and calculate the result
def process_addition(query):
    try:
        # Get model response
        model_response = call_llama_model(query)
        
        # Create a debug output with the raw response
        debug_info = f"Raw model response:\n{model_response}\n\n"
        
        # Try to parse the JSON response
        try:
            # Find the JSON part in the response (it might have additional text)
            json_start = model_response.find('{')
            json_end = model_response.rfind('}') + 1
            
            if json_start >= 0 and json_end > json_start:
                json_str = model_response[json_start:json_end]
                response_data = json.loads(json_str)
                debug_info += f"Parsed JSON:\n{json.dumps(response_data, indent=2)}\n\n"
            else:
                return f"Error: No valid JSON found in response.\n\n{debug_info}"
            
            # Check if it has a function call
            if "function_call" in response_data:
                function_name = response_data["function_call"]["name"]
                params = response_data["function_call"]["parameters"]
                
                if function_name == "add_numbers":
                    # Extract the numbers
                    num1 = float(params["num1"])
                    num2 = float(params["num2"])
                    result = num1 + num2
                    
                    # Return a formatted response
                    return f"""
### Input Processed Successfully

**Numbers extracted:** {num1} and {num2}

**Function called:** `{function_name}`

**Result:** {result}

**JSON Function Call:**
```json
{json.dumps(response_data, indent=2)}