File size: 5,414 Bytes
9b5b26a
272f7ee
9b5b26a
 
 
c19d193
cc1fa82
56c4b64
6aae614
8fe992b
9b5b26a
 
6c3c664
 
5df72d6
56c4b64
 
 
 
 
 
 
 
 
 
 
9b5b26a
48155d6
 
9b5b26a
48155d6
 
9b5b26a
6c3c664
48155d6
 
272f7ee
6c3c664
272f7ee
 
6c3c664
6aa9465
48155d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272f7ee
9b5b26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
 
6aae614
ae7a494
 
 
 
e121372
bf6d34c
 
806020c
fe328e0
6c3c664
13d500a
8c01ffb
 
9b5b26a
 
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
0c9f97f
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
9b5b26a
8c01ffb
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
133
134
135
136
137
138
139
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
from transformers import pipeline
import datetime
import requests
import pytz
import yaml
import os
import torch
from tools.final_answer import FinalAnswerTool

from Gradio_UI import GradioUI

rapid_api_key=os.environ["RAPID_KEY"]

# Below is an example of a tool that does nothing. Amaze us with your creativity !
#@tool
#def reflecting_on_results(arg1: str) -> str:
#    """A tool that receives the football teams comparison and then it reflects on the results with an insightful comment, it could be funny or witty
#    Args:
#        arg1: The first argument receives a string with the comparison of two teams as queried by user
#    """
#    insights
#    return insights



@tool
def get_football_results(team1: str, team2: str) -> str:
    """A tool that connects to an API that provides live football data and compares the stats of two teams in the English Premier League, then uses an LLM to generate a reflection on how both teams stack up.
    Args:
        team1: The first football team to check
        team2: The second football team to check
    """

    # First attempt to get actual data from the API
    url = "https://free-api-live-football-data.p.rapidapi.com/football-get-standing-home"
    querystring = {"leagueid": "47"}  # British Premier League
    headers = {
        "x-rapidapi-key": rapid_api_key,
        "x-rapidapi-host": "free-api-live-football-data.p.rapidapi.com"
    }

    # Try to get the data from the API
    try:
        response = requests.get(url, headers=headers, params=querystring)
        response_text = response.text  # Get the raw response as text

        # Load the TinyLlama model for analysis
        tiny_llama = pipeline("text-generation",
                              model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
                              torch_dtype=torch.bfloat16,
                              device_map="auto")

        # Create the prompt for the LLM
        messages = [
            {
                "role": "system",
                "content": f"You are a friendly AI system embedded in a wider AI agent. You are going to facilitate the work of other LLMs. You have received from user an API response in JSON format. In the response item 28 there is a long text starting with a text like 'status:success,response...You are going to extract the team names from the response and compare the stats of the two teams. First team check is {team1} and second team is {team2}. Provide a funny reflection on your analysis of how these two teams stack up against each other. You should provide a text response"
            },
            {
                "role": "user",
                "content": response_text
            }
        ]

        # Apply chat template and generate response
        prompt = tiny_llama.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
        output = tiny_llama(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)

        # Extract the generated text
        comparison = output[0]['generated_text']

        # Return the comparison as is (it's already a string)
        return comparison

    except Exception as e:
        # Handle errors gracefully
        return f"""
## {team1} vs {team2} Premier League Comparison

I couldn't complete the analysis due to an error: {str(e)}

Let me provide some general information instead:

Both {team1} and {team2} are prominent teams in the English Premier League. To get a detailed comparison with current statistics, please try again later when the API and LLM processing are working properly.
"""

      

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    Args:
        timezone: A string representing a valid timezone (e.g., 'America/New_York').
    """
    try:
        # Create timezone object
        tz = pytz.timezone(timezone)
        # Get current time in that timezone
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"


final_answer = FinalAnswerTool()

# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 

model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/QwQ-32B',# it is possible that this model may be overloaded, funny how it works with Qwen/QwQ-32B, default is Qwen/Qwen2.5-Coder-32B-Instruct
custom_role_conversions=None,
token=os.environ["HF_TOKEN"]
)


# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer, get_current_time_in_timezone, get_football_results], ## add your tools here (don't remove final answer)
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)


GradioUI(agent).launch()