File size: 2,817 Bytes
7336908
 
 
184dc24
 
7336908
 
184dc24
 
fffd8eb
94099fe
7336908
fb8b406
 
7336908
 
 
fb8b406
7336908
 
 
 
 
 
 
 
fffd8eb
7336908
 
 
 
 
 
 
 
 
b143b9f
7336908
 
 
 
 
 
 
318feab
20e5c62
318feab
7336908
6bd4839
867b94b
7336908
20e5c62
 
 
 
 
7336908
 
 
e74b982
7336908
 
 
20e5c62
7336908
867b94b
20e5c62
7336908
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
from magentic import FunctionCall, OpenaiChatModel, prompt_chain
import requests

from BasicAgent import BasicAgent

import os


class CustomAgent(BasicAgent):
    def __init__(self):
        super().__init__()
        self._system_prompt = """\
You are a general AI assistant. I will ask you a question. Report your thoughts and provide an answer using, if appropriate, the tools at your disposal.
Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. Do not provide anything except your final answer.
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. 
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
Question: {question}
""" # System prompt, adapted from the GAIA team's example at https://huggingface.co/spaces/gaia-benchmark/leaderboard
        self._model = OpenaiChatModel(
            model="llama-3.3-70b-versatile",
            api_key=os.environ["GROQ_API_KEY"],
            base_url="https://api.groq.com/openai/v1/", 
            temperature=0.2, 
            max_tokens=512
        )
        print("CustomAgent initialized.")

    def __call__(self, question: str) -> str:
        """
        this is where the magic happens
        """
        super().__call__(question)
        @prompt_chain(
            template=self._system_prompt,
            model=self._model,
            functions=[self.websearch],
        )
        def _magic(question: str) -> FunctionCall | str:
            """
            An agent, using websearch to gather information and
            answer a user's query.
            """
            ...
        result = _magic(question)
        print("Agent returning actual answer:", result)
        return result
    
    @staticmethod
    def websearch(self, query: str) -> str:
        """
        Performs a web search using an external web search API.
        Args:
            query (str): The search query string.
        Returns:
            str: The response from the web search API as a string.
        """
        try:
            r = requests.post(
                f"{os.environ['WEBSEARCH_API_URL']}/tools/websearch",
                json={"query": query}
            )
            r.raise_for_status()
            response = r.text
        except Exception as e:
            print("Error:", e)
            response = "An error has occurred while using websearch, please inform the user of this"
        print(f"Agent searched for {query}. Response: {response}")