Spaces:
Sleeping
Sleeping
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)} |