Categories
RAG

Llama3 PDF RAG using PhiData

Groq PDF RAG using PhiData

~/phidata/cookbook/llms/groq/rag

1. Setup Environment and Install packages

git clone https://github.com/phidatahq/phidata
cd phidata/cookbook/llms/groq/rag
conda create -n phidata python=3.11 -y
conda activate phidata
pip install -r requirements.txt
export GROQ_API_KEY=xxxxxxxxxx

2. Start Database

Download: Docker Desktop https://www.docker.com/products/docker-desktop/

Test if Docker got downloaded

docker ps
docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  phidata/pgvector:16

Test if the got database started

docker ps

3. Start Application

streamlit run app.py

Ollama PDF RAG using Phi Data

~/phidata/cookbook/llms/ollama/rag
cd phidata/cookbook/llms/ollama/rag
pip install -r requirements.txt
ollama pull llama3
streamlit run app.py
Categories
AI Agents

CrewAI Groq Llama 3: Sports News Agency

Install

conda create -n crewai python=3.11 -y
conda activate crewai
pip install 'crewai[tools]' flask requests

Create DB Code (Optional)

Optional, if you already have a DB with data.

import sqlite3

def create_db():
    conn = sqlite3.connect('nba_games.db')
    c = conn.cursor()
    c.execute('''
        CREATE TABLE IF NOT EXISTS games (
            team_name TEXT,
            game_id TEXT,
            status TEXT,
            home_team TEXT,
            home_team_score INTEGER,
            away_team TEXT,
            away_team_score INTEGER
        )
    ''')
    games = [
        ("warriors", "401585601", "Final", "Los Angeles Lakers", 121, "Golden State Warriors", 128),
        ("lakers", "401585601", "Final", "Los Angeles Lakers", 121, "Golden State Warriors", 128),
        ("nuggets", "401585577", "Final", "Miami Heat", 88, "Denver Nuggets", 100),
        ("heat", "401585577", "Final", "Miami Heat", 88, "Denver Nuggets", 100)
    ]
    c.executemany('INSERT INTO games (team_name, game_id, status, home_team, home_team_score, away_team, away_team_score) VALUES (?, ?, ?, ?, ?, ?, ?)', games)
    conn.commit()
    conn.close()

create_db()

API Code

from flask import Flask, request, jsonify
import sqlite3

app = Flask(__name__)

def get_game_score(team_name):
    conn = sqlite3.connect('nba_games.db')
    c = conn.cursor()
    team_name = team_name.lower()
    c.execute('SELECT * FROM games WHERE team_name = ?', (team_name,))
    result = c.fetchone()
    conn.close()
    if result:
        keys = ["game_id", "status", "home_team", "home_team_score", "away_team", "away_team_score"]
        return dict(zip(keys, result[1:]))
    else:
        return {"team_name": team_name, "score": "unknown"}

@app.route('/')
def home():
    return jsonify({
        'message': 'Welcome to the NBA Scores API. Use /score?team=<team_name> to fetch game scores.'
    })

@app.route('/score', methods=['GET'])
def score():
    team_name = request.args.get('team', '')
    if not team_name:
        return jsonify({'error': 'Missing team name'}), 400
    score = get_game_score(team_name)
    return jsonify(score)

if __name__ == '__main__':
    app.run(debug=True)

CrewAI Code

import os
import json
import requests
from crewai import Agent, Task, Crew
from crewai_tools import BaseTool

# 1. Create Custom Tool to Get Game Score from API
from crewai_tools import tool
@tool("Game Score Tool")
def game_score_tool(team_name: str) -> str:
    """Get the current score for a given NBA game by querying the Flask API. It accepts team_name"""
    url = f'http://127.0.0.1:5000/score?team={team_name}'
    response = requests.get(url)
    if response.status_code == 200:
        return json.dumps(response.json(), indent=2)
    else:
        return json.dumps({"error": "API request failed", "status_code": response.status_code}, indent=2)

# 2. Create Agents
researcher = Agent(
    role='Researcher',
    goal='Gather and analyze information on NBA game scores',
    verbose=True,
    backstory=(
        "As a seasoned researcher, you have a keen eye for detail and a "
        "deep understanding of sports analytics. You're adept at sifting through "
        "scores to find the most relevant and accurate data."
    ),
    tools=[game_score_tool],
    allow_delegation=False
)

writer = Agent(
    role='Sports Journalist',
    goal='Compose an engaging news article based on NBA game scores',
    verbose=True,
    backstory=(
        "With a talent for storytelling, you convert statistical data and game outcomes "
        "into engaging sports narratives. Your articles are insightful, capturing the excitement "
        "of the games and providing a deep analysis for sports enthusiasts."
    ),
    allow_delegation=False
)

# 3. Define Tasks
research_task = Task(
    description="Investigate the scores for the Warriors game.",
    expected_output='A detailed report summarizing the data.',
    tools=[game_score_tool],
    agent=researcher,
)

writing_task = Task(
    description="Write a detailed news article about an NBA game, focusing stats.",
    expected_output='An engaging and informative article suitable for publication in sports media.',
    context=[research_task],
    agent=writer,
)

# 4. Run the Crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task]
)

result = crew.kickoff()
print(result)
Categories
RAG

Llama 3 RAG using Ollama

Install

pip install ollama langchain beautifulsoup4 chromadb gradio
ollama pull llama3
ollama pull nomic-embed-text

Code

import ollama
import bs4
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OllamaEmbeddings
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

# 1. Load the data
loader = WebBaseLoader(
    web_paths=("http://localhost/llm.html",),
    bs_kwargs=dict(
        parse_only=bs4.SoupStrainer(
            class_=("post-content", "post-title", "post-header")
        )
    ),
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)

# 2. Create Ollama embeddings and vector store
embeddings = OllamaEmbeddings(model="llama3")
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)

# 3. Call Ollama Llama3 model
def ollama_llm(question, context):
    formatted_prompt = f"Question: {question}\n\nContext: {context}"
    response = ollama.chat(model='llama3', messages=[{'role': 'user', 'content': formatted_prompt}])
    return response['message']['content']

# 4. RAG Setup
retriever = vectorstore.as_retriever()
def combine_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)
def rag_chain(question):
    retrieved_docs = retriever.invoke(question)
    formatted_context = combine_docs(retrieved_docs)
    return ollama_llm(question, formatted_context)

# 5. Use the RAG App
result = rag_chain("What is Task Decomposition?")
print(result)

UI

import gradio as gr
import bs4
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OllamaEmbeddings
import ollama

# Function to load, split, and retrieve documents
def load_and_retrieve_docs(url):
    loader = WebBaseLoader(
        web_paths=(url,),
        bs_kwargs=dict() 
    )
    docs = loader.load()
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    splits = text_splitter.split_documents(docs)
    embeddings = OllamaEmbeddings(model="nomic-embed-text")
    vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
    return vectorstore.as_retriever()

# Function to format documents
def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

# Function that defines the RAG chain
def rag_chain(url, question):
    retriever = load_and_retrieve_docs(url)
    retrieved_docs = retriever.invoke(question)
    formatted_context = format_docs(retrieved_docs)
    formatted_prompt = f"Question: {question}\n\nContext: {formatted_context}"
    response = ollama.chat(model='llama3', messages=[{'role': 'user', 'content': formatted_prompt}])
    return response['message']['content']

# Gradio interface
iface = gr.Interface(
    fn=rag_chain,
    inputs=["text", "text"],
    outputs="text",
    title="RAG Chain Question Answering",
    description="Enter a URL and a query to get answers from the RAG chain."
)

# Launch the app
iface.launch()
Categories
LLM Ollama

Llama 3 Run Locally: Ollama, LM Studio, Jan AI

Ollama

import ollama

stream = ollama.chat(
    model='llama3',
    messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
    stream=True,
)

for chunk in stream:
  print(chunk['message']['content'], end='', flush=True)

LM Studio

# Example: reuse your existing OpenAI setup
from openai import OpenAI

# Point to the local server
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

completion = client.chat.completions.create(
  model="model-identifier",
  messages=[
    {"role": "system", "content": "Always answer in rhymes."},
    {"role": "user", "content": "Introduce yourself."}
  ],
  temperature=0.7,
)

print(completion.choices[0].message)

Jan AI

import requests
import json

# Endpoint URL
url = "http://localhost:1337/v1/chat/completions"

# Headers
headers = {
    "Content-Type": "application/json",
}

# Payload
payload = {
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Give me meal plan for today."},
    ],
    "model": "llama2-chat-7b-q4",
    "stream": False,
    "max_tokens": 2048,
    "stop": None,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "temperature": 0.7,
    "top_p": 0.95
}

# Make the request
response = requests.post(url, headers=headers, data=json.dumps(payload))

# Extracting 'content' value from response
result = response.json()
content = result['choices'][0]['message']['content']
print(content)
Categories
Tools

Groq Tools Llama3: Create AI App with API Integration

pip install groq requests flask
export GROQ_API_KEY=xxxxxxxxxxx
from groq import Groq
import os
import json
import requests

client = Groq(api_key = os.getenv('GROQ_API_KEY'))
MODEL = 'llama3-70b-8192'

def get_game_score(team_name):
    """Get the current score for a given NBA game by querying the Flask API."""
    url = f'http://127.0.0.1:5000/score?team={team_name}'
    response = requests.get(url)
    if response.status_code == 200:
        return json.dumps(response.json())
    else:
        return json.dumps({"error": "API request failed", "status_code": response.status_code})

def run_conversation(user_prompt):
    # Step 1: send the conversation and available functions to the model
    messages=[
        {
            "role": "system",
            "content": "You are a function calling LLM that uses the data extracted from the get_game_score function to answer questions around NBA game scores. Include the team and their opponent in your response."
        },
        {
            "role": "user",
            "content": user_prompt,
        }
    ]
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_game_score",
                "description": "Get the score for a given NBA game",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "team_name": {
                            "type": "string",
                            "description": "The name of the NBA team (e.g. 'Golden State Warriors')",
                        }
                    },
                    "required": ["team_name"],
                },
            },
        }
    ]
    response = client.chat.completions.create(
        model=MODEL,
        messages=messages,
        tools=tools,
        tool_choice="auto",  
        max_tokens=4096
    )

    response_message = response.choices[0].message
    tool_calls = response_message.tool_calls
    # Step 2: check if the model wanted to call a function
    if tool_calls:
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            "get_game_score": get_game_score,
        }  # only one function in this example, but you can have multiple
        messages.append(response_message)  # extend conversation with assistant's reply
        # Step 4: send the info for each function call and function response to the model
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = json.loads(tool_call.function.arguments)
            function_response = function_to_call(
                team_name=function_args.get("team_name")
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )  # extend conversation with function response
        second_response = client.chat.completions.create(
            model=MODEL,
            messages=messages
        )  # get a new response from the model where it can see the function response
        return second_response.choices[0].message.content
    
user_prompt = "What was the score of the Warriors game?"
print(run_conversation(user_prompt))

API

from flask import Flask, request, jsonify
import json

app = Flask(__name__)

def get_game_score(team_name):
    """Get the current score for a given NBA game"""
    team_name = team_name.lower()
    if "warriors" in team_name:
        return {"game_id": "401585601", "status": 'Final', "home_team": "Los Angeles Lakers", "home_team_score": 121, "away_team": "Golden State Warriors", "away_team_score": 128}
    elif "lakers" in team_name:
        return {"game_id": "401585601", "status": 'Final', "home_team": "Los Angeles Lakers", "home_team_score": 121, "away_team": "Golden State Warriors", "away_team_score": 128}
    elif "nuggets" in team_name:
        return {"game_id": "401585577", "status": 'Final', "home_team": "Miami Heat", "home_team_score": 88, "away_team": "Denver Nuggets", "away_team_score": 100}
    elif "heat" in team_name:
        return {"game_id": "401585577", "status": 'Final', "home_team": "Miami Heat", "home_team_score": 88, "away_team": "Denver Nuggets", "away_team_score": 100}
    else:
        return {"team_name": team_name, "score": "unknown"}

@app.route('/score', methods=['GET'])
def score():
    team_name = request.args.get('team', '')
    if not team_name:
        return jsonify({'error': 'Missing team name'}), 400
    score = get_game_score(team_name)
    return jsonify(score)

if __name__ == '__main__':
    app.run(debug=True)
curl http://127.0.0.1:5000/score?team=lakers
Categories
Finetuning

Llama 3 Fine Tune with Custom Data

pip install huggingface_hub ipython "unsloth[colab] @ git+https://github.com/unslothai/unsloth.git" "unsloth[conda] @ git+https://github.com/unslothai/unsloth.git"
export HF_TOKEN=xxxxxxxxxxxxx

wandb optional

pip install wandb
wandb login
# 1. Importing and configurations 
import os
from unsloth import FastLanguageModel
import torch
from trl import SFTTrainer
from transformers import TrainingArguments
from datasets import load_dataset

max_seq_length = 2048
url = "https://huggingface.co/datasets/laion/OIG/resolve/main/unified_chip2.jsonl"
dataset = load_dataset("json", data_files = {"train" : url}, split = "train")

# 2. Load Llama3 model
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/llama-3-8b-bnb-4bit",
    max_seq_length = max_seq_length,
    dtype = None,
    load_in_4bit = True,
)

# 3 Before training
def generate_text(text):
    inputs = tokenizer(text, return_tensors="pt").to("cuda:0")
    outputs = model.generate(**inputs, max_new_tokens=20)
    print(tokenizer.decode(outputs[0], skip_special_tokens=True))

print("Before training\n")
generate_text("<human>: List the top 5 most popular movies of all time.\n<bot>: ")

# 4. Do model patching and add fast LoRA weights and training
model = FastLanguageModel.get_peft_model(
    model,
    r = 16,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj",],
    lora_alpha = 16,
    lora_dropout = 0, # Supports any, but = 0 is optimized
    bias = "none",    # Supports any, but = "none" is optimized
    use_gradient_checkpointing = True,
    random_state = 3407,
    max_seq_length = max_seq_length,
    use_rslora = False,  # Rank stabilized LoRA
    loftq_config = None, # LoftQ
)

trainer = SFTTrainer(
    model = model,
    train_dataset = dataset,
    dataset_text_field = "text",
    max_seq_length = max_seq_length,
    tokenizer = tokenizer,
    args = TrainingArguments(
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 4,
        warmup_steps = 10,
        max_steps = 60,
        fp16 = not torch.cuda.is_bf16_supported(),
        bf16 = torch.cuda.is_bf16_supported(),
        logging_steps = 1,
        output_dir = "outputs",
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
    ),
)
trainer.train()

# 5. After training
print("\n ######## \nAfter training\n")
generate_text("<human>: List the top 5 most popular movies of all time.\n<bot>: ")

# 6. Save the model
model.save_pretrained("lora_model")
model.save_pretrained_merged("outputs", tokenizer, save_method = "merged_16bit",)
model.push_to_hub_merged("YOURUSERNAME/llama3-8b-oig-unsloth-merged", tokenizer, save_method = "merged_16bit", token = os.environ.get("HF_TOKEN"))
model.push_to_hub("YOURUSERNAME/llama3-8b-oig-unsloth", tokenizer, save_method = "lora", token = os.environ.get("HF_TOKEN"))
Categories
AutoGen

AutoGen Custom Tools

import math
import os
from typing import Optional, Type
from langchain_community.chat_models import ChatOpenAI
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool
from langchain.tools.file_management.read import ReadFileTool
import autogen

# 1. Create Circumference Tool
class CircumferenceToolInput(BaseModel):
    radius: float = Field()

class CircumferenceTool(BaseTool):
    name = "circumference_calculator"
    description = "Use this tool when you need to calculate a circumference using the radius of a circle"
    args_schema: Type[BaseModel] = CircumferenceToolInput

    def _run(self, radius: float):
        return float(radius) * 2.0 * math.pi

# 2. Get File Path for radius file (Used by Inbuilt ReadFileTool)
def get_file_path_radius():
    current_dir = os.path.dirname(__file__)
    file_path = os.path.join(current_dir, "radius.txt")
    return file_path

# 3. LLM Config 
def generate_llm_config(tool):
    function_schema = {
        "name": tool.name.lower().replace(" ", "_"),
        "description": tool.description,
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
        },
    }

    if tool.args is not None:
        function_schema["parameters"]["properties"] = tool.args

    return function_schema


# Instantiate the ReadFileTool
read_file_tool = ReadFileTool()
custom_tool = CircumferenceTool()

# Construct the llm_config
llm_config = {
    "functions": [
        generate_llm_config(custom_tool),
        generate_llm_config(read_file_tool),
    ],
    "config_list": [{"model": os.environ["OPENAI_MODEL_NAME"], "api_key": os.environ["OPENAI_API_KEY"], "base_url": os.environ["OPENAI_API_BASE"]}],
    "timeout": 120,
}

# 4. Creating User Agent and Registering Tools
user = autogen.UserProxyAgent(
    name="user",
    is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    code_execution_config={
        "work_dir": "coding",
        "use_docker": False,
    },
)

user.register_function(
    function_map={
        custom_tool.name: custom_tool._run,
        read_file_tool.name: read_file_tool._run,
    }
)

# 5. Create Circumference Agent and Initiate Chat
CircumferenceAgent = autogen.AssistantAgent(
    name="Circumference Agent",
    system_message="For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.",
    llm_config=llm_config,
)

user.initiate_chat(
    CircumferenceAgent,
    message=f"Read the file with the path {get_file_path_radius()}, then calculate the circumference of a circle that has a radius of that files contents.",  # 7.81mm in the file
    llm_config=llm_config,
)
Categories
AI Agents

CrewAI Custom Tools

import os
from crewai import Agent, Task, Crew
from duckduckgo_search import DDGS
from langchain.tools import tool

@tool("Internet Search Tool")
def internet_search_tool(query: str) -> list:
    """Search Internet for relevant information based on a query."""
    ddgs = DDGS()
    results = ddgs.text(keywords=query, region='wt-wt', safesearch='moderate', max_results=5)
    return results

# Define agents
researcher = Agent(
    role='Researcher',
    goal='Gather and analyze information on specific topics',
    verbose=True,
    backstory=(
        "As a seasoned researcher, you have a keen eye for detail and a "
        "deep understanding of your field. You're adept at sifting through "
        "information to find the most relevant and accurate data."
    ),
    tools=[internet_search_tool],
    allow_delegation=True
)

writer = Agent(
    role='Writer',
    goal='Compose informative and engaging articles based on research findings',
    verbose=True,
    backstory=(
        "With a talent for storytelling, you translate complex ideas into "
        "clear, compelling narratives. Your articles are well-researched, "
        "thought-provoking, and accessible to a broad audience."
    ),
    tools=[internet_search_tool],
    allow_delegation=False
)

# Define tasks
research_task = Task(
    description=(
        "Investigate the latest trends in renewable energy technologies. "
        "Identify key advancements, challenges, and potential impacts on "
        "global energy policies."
    ),
    expected_output='A detailed report summarizing the findings.',
    tools=[internet_search_tool],
    agent=researcher,
)

writing_task = Task(
    description=(
        "Based on the research findings, write an article that highlights "
        "the importance of renewable energy innovations. The article should "
        "educate the public on how these technologies can contribute to "
        "sustainable development."
    ),
    expected_output='An engaging and informative article suitable for publication.',
    tools=[internet_search_tool],
    agent=writer,
)

# Create and kick off the crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task]
)

result = crew.kickoff(inputs={'topic': 'renewable energy'})
print(result)
Categories
AutoGen

AutoGen LangChain Tools

import math
import os
from typing import Optional, Type

# Starndard Langchain example
from langchain.agents import create_spark_sql_agent
from langchain.agents.agent_toolkits import SparkSQLToolkit
from langchain.chat_models import ChatOpenAI

# Import things that are needed generically
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool
from langchain.tools.file_management.read import ReadFileTool
from langchain.utilities.spark_sql import SparkSQL
# from pyspark.sql import SparkSession

import autogen

class CircumferenceToolInput(BaseModel):
    radius: float = Field()


class CircumferenceTool(BaseTool):
    name = "circumference_calculator"
    description = "Use this tool when you need to calculate a circumference using the radius of a circle"
    args_schema: Type[BaseModel] = CircumferenceToolInput

    def _run(self, radius: float):
        return float(radius) * 2.0 * math.pi


def get_file_path_of_example():
    current_dir = os.path.dirname(__file__)
    file_path = os.path.join(current_dir, "radius.txt")
    return file_path

# Define a function to generate llm_config from a LangChain tool
def generate_llm_config(tool):
    # Define the function schema based on the tool's args_schema
    function_schema = {
        "name": tool.name.lower().replace(" ", "_"),
        "description": tool.description,
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
        },
    }

    if tool.args is not None:
        function_schema["parameters"]["properties"] = tool.args

    return function_schema


# Instantiate the ReadFileTool
read_file_tool = ReadFileTool()
custom_tool = CircumferenceTool()

# Construct the llm_config
llm_config = {
    # Generate functions config for the Tool
    "functions": [
        generate_llm_config(custom_tool),
        generate_llm_config(read_file_tool),
    ],
    "config_list": [{"model": "gpt-3.5-turbo", "api_key": os.environ["OPENAI_API_KEY"]}],
    "timeout": 120,
}

user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    code_execution_config={
        "work_dir": "coding",
        "use_docker": False,
    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
)

# Register the tool and start the conversation
user_proxy.register_function(
    function_map={
        custom_tool.name: custom_tool._run,
        read_file_tool.name: read_file_tool._run,
    }
)

chatbot = autogen.AssistantAgent(
    name="chatbot",
    system_message="For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.",
    llm_config=llm_config,
)

user_proxy.initiate_chat(
    chatbot,
    message=f"Read the file with the path {get_file_path_of_example()}, then calculate the circumference of a circle that has a radius of that files contents.",  # 7.81mm in the file
    llm_config=llm_config,
)

radius.txt

7.81mm
Categories
Tools

Anthropic Tools: Text Classification

from anthropic import Anthropic
import requests
from bs4 import BeautifulSoup
import json

client = Anthropic()
MODEL_NAME = "claude-3-haiku-20240307"

# 4. Text Classification
tools = [
    {
        "name": "print_classification",
        "description": "Prints the classification results.",
        "input_schema": {
            "type": "object",
            "properties": {
                "categories": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "name": {"type": "string", "description": "The category name."},
                            "score": {"type": "number", "description": "The classification score for the category, ranging from 0.0 to 1.0."}
                        },
                        "required": ["name", "score"]
                    }
                }
            },
            "required": ["categories"]
        }
    }
]

text = "The new quantum computing breakthrough could revolutionize the tech industry."

query = f"""
<document>
{text}
</document>

Use the print_classification tool. The categories can be Politics, Sports, Technology, Entertainment, Business.
"""

response = client.beta.tools.messages.create(
    model=MODEL_NAME,
    max_tokens=4096,
    tools=tools,
    messages=[{"role": "user", "content": query}]
)

json_classification = None
for content in response.content:
    if content.type == "tool_use" and content.name == "print_classification":
        json_classification = content.input
        break

if json_classification:
    print("Text Classification (JSON):")
    print(json.dumps(json_classification, indent=2))
else:
    print("No text classification found in the response.")