File size: 3,043 Bytes
7336908
 
 
184dc24
 
7336908
 
184dc24
 
fffd8eb
94099fe
7336908
fe78835
0fe79e1
fe78835
7336908
 
 
fb8b406
7336908
 
df0be25
7336908
 
0fe79e1
7336908
 
fffd8eb
7336908
 
 
 
 
c1f8728
7336908
 
 
df0be25
7336908
 
 
 
 
 
 
318feab
20e5c62
318feab
df0be25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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. Provide an answer like so: YOUR FINAL ANSWER. DO NOT SAY ANYTHING EXCEPT YOUR FINAL ANSWER. 
Only use the provided tools if your own knowledge would be insufficient. If you are unable to answer the question using the provided tools, give your best guess.
Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
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.1-8b-instant",
            api_key=os.environ["GROQ_API_KEY"],
            base_url="https://api.groq.com/openai/v1/", 
            temperature=0.7, 
            max_tokens=512
        )
        print("CustomAgent initialized.")

    def __call__(self, question: str) -> str:
        """
        this is where the magic happens
        """
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        @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(query: str) -> str:
        """
        Performs a web search using an external web search API. Note that better results may be obtained from varying the wording of the query.
        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}")