Tuesday, July 23, 2024

Llama 3 Groq 70B Tool Use Model - Local Installation and Function Calling

 This video installs Llama-3-Groq-8B-Tool-Use locally which is specifically designed for advanced tool use and function calling tasks.


Code:


conda create -n groqllama python=3.11 -y && conda activate groqllama

conda install jupyter -y
pip uninstall charset_normalizer -y
pip install charset_normalizer
jupyter notebook

pip install torch transformers sentencepiece accelerate huggingface_hub tavily-python
export TAVILY_API_KEY=""


import transformers
import torch
import os
import re
import json
from tavily import TavilyClient
tavily_client = TavilyClient(api_key=os.getenv('TAVILY_API_KEY'))

import warnings
warnings.filterwarnings('ignore')

model_id = "Groq/Llama-3-Groq-8B-Tool-Use"

pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device_map="auto",
)

prompt ="""

<|start_header_id|>system<|end_header_id|>

You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
<tool_call>
{"name": <function-name>,"arguments": <args-dict>}
</tool_call>

Here are the available tools:
<tools> {
  "name": "get_current_weather",
  "description": "Get the current weather in a given location",
  "parameters": {
    "type": "object",
    "properties": {
      "location": {
        "type": "string",
        "description": "The city and state, e.g. San Francisco, CA"
      },
      "format": {
        "type": "string",
        "description": "The temperature unit to use. Infer this from the users location.",
        "enum": [
          "celsius",
          "fahrenheit"
        ]
      }
    },
    "required": [
      "location",
      "format"
    ]
  }
} </tools><|eot_id|><|start_header_id|>user<|end_header_id|>

What is the weather like in Sydney in Celsius?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

"""

response = pipeline(prompt)

# Use a regex pattern to find the tool call JSON
generated_text = response[0].get('generated_text', '')

# Use a regex pattern to find the tool call JSON
tool_call_match = re.search(r'\{.*?\}', generated_text, re.DOTALL)

if tool_call_match:
    tool_call_json = tool_call_match.group(0)
   
    # Correctly format the JSON string
    tool_call_json = tool_call_json.replace("<function-name>", "get_current_weather")  # Replace placeholder
    tool_call_json = tool_call_json.replace("<args-dict>", '{"location": "Sydney, NSW", "format": "celsius"}')  # Replace placeholder
    tool_call_json = tool_call_json.replace("'", '"')  # Replace single quotes with double quotes
    tool_call_json = tool_call_json.replace('name:', '"name":')  # Ensure proper quoting for keys
    tool_call_json = tool_call_json.replace('arguments:', '"arguments":')

    # Ensure proper quoting of all parts of the JSON string
    tool_call_json = tool_call_json.replace('"name": get_current_weather', '"name": "get_current_weather"')

    # Debug: Print the extracted JSON string
    #print(f"Extracted JSON: {tool_call_json}")
   
    # Correctly format the JSON string
    try:
        tool_call = json.loads(tool_call_json)
        print(tool_call)
    except json.JSONDecodeError as e:
        print(f"Error decoding JSON: {e}")
        # Debug: Print the exact content that failed to parse
        print(f"Failed JSON content: {tool_call_json}")
else:
    print("No tool call JSON found.")
 

location=tool_call['arguments']['location']
format_unit=tool_call['arguments']['format']
query = f"current weather in {location} in {format_unit}"
response = tavily_client.search(query)
print(response)

No comments: