File size: 3,497 Bytes
fb293f8
59f5166
00aced7
ee397c8
1c7a0a7
 
 
 
230d96d
ce7387d
230d96d
df58f18
230d96d
 
3bd69bd
fca87d5
 
 
 
7ddb52b
f407c48
fca87d5
 
 
f407c48
 
fca87d5
 
f407c48
fca87d5
923b5cd
 
f407c48
 
 
 
230d96d
7bc23ab
ce7387d
1c7a0a7
 
 
7f78cfc
00aced7
7f78cfc
 
fcc83a7
1c7a0a7
 
7f78cfc
 
 
 
 
 
 
 
1c7a0a7
 
7f78cfc
 
 
1c7a0a7
069476b
7f78cfc
 
 
 
fcc83a7
 
23b92b8
7f78cfc
 
7311d82
 
ab1c2b7
 
e527469
59977e3
e527469
ee397c8
e527469
 
ee397c8
7f78cfc
e527469
ee397c8
 
7f78cfc
ee397c8
 
 
 
d94acaf
ee397c8
 
 
 
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
import os

from crewai import Agent, Crew, Process, Task
from crewai.tools import tool
from crewai_tools import (
    SerperDevTool,
    WebsiteSearchTool
)
from phoenix.otel import register

PHOENIX_API_KEY = os.environ["PHOENIX_API_KEY"]

os.environ["PHOENIX_CLIENT_HEADERS"] = f"api_key={PHOENIX_API_KEY}"
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = "https://app.phoenix.arize.com"

#tracer_provider = register(
#    auto_instrument=True,
#    project_name="gaia"
#)

###
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from openinference.instrumentation.crewai import CrewAIInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

endpoint = "https://app.phoenix.arize.com/v1/traces"
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))

CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
###

#agentops.init(os.environ["AGENTOPS_API_KEY"])

def get_crew():
    search_tool = SerperDevTool()
    web_rag_tool = WebsiteSearchTool()
    
    research_agent = Agent(
        role="Web Research Agent",
        goal="Search the web for topic \"{topic}\" and scrape the most relevant web page.",
        backstory="As an expert web research assistant, you are searching the web for topic \"{topic}\" and scraping the most relevant web page.",
        tools=[search_tool, web_rag_tool],
        verbose=True
    )

    answer_agent = Agent(
        role="Final Answer Agent",
        goal="Provide the final answer on topic \"{topic}\".",
        backstory="As an expert answer assistant, you are providing the final answer on topic \"{topic}\".",
        tools=[final_answer_tool],
        verbose=True
    )
    
    research = Task(
        agent=research_agent,
        description="Search the web for topic \"{topic}\" and scrape the most relevant web page.",
        expected_output="Content on topic \"{topic}\"."
    )

    answer = Task(
        agent=answer_agent,
        description="Given topic \"{topic}\" and an initial answer, get the final answer.",
        expected_output="The final answer to topic \"{topic}\"."
    )
    
    return Crew(
        agents=[research_agent, answer_agent],
        tasks=[research, answer],
        planning=True,
        verbose=True
    )

@tool("Final answer tool.")
def final_answer_tool(question: str, answer: str) -> str:
    """Given a question and an initial answer, provide the final answer."""
    prompt_template = """
        You are given a question and an initial answer. You must **precisely** answer the question based on the initial answer.
        If asked for a number, only provide the number. Do not include explanations, steps, reasoning, or additional text.
        **Question:** """ + question + """
        **Initial answer:** """ + answer + """
        **Example 1:** How many states are in the USA? 50
        **Example 2:** What is the superlative of good? Best
        **Example 3:** What is the opposite of left? Right
        **Final answer:**:
        """

    client = OpenAI()
    completion = client.chat.completions.create(
        messages = [{"role": "user", "content": [{"type": "text", "text": prompt_template}]}],
        model = "gpt-4o-mini"
    )
        
    return completion.choices[0].message.content