-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
45 lines (37 loc) · 1.73 KB
/
agent.py
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
import os
from dotenv import load_dotenv
from openai import OpenAI
# Load environment variables from .env
load_dotenv()
# Instantiate the client with your API key
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def summarization_agent(text):
"""
Sends a summarization prompt to an LLM using the new ChatCompletion interface.
"""
prompt = f"Please summarize the following text:\n\n{text}\n\nSummary:"
response = client.chat.completions.create(
model="gpt-4o", # Or another model like "gpt-3.5-turbo"
messages=[{"role": "user", "content": prompt}],
max_tokens=100,
temperature=0.3
)
return response.choices[0].message.content.strip()
def evaluate_summarization():
test_text = (
"Climate change is one of the most significant challenges of our time. "
"Rising global temperatures have led to extreme weather events, melting polar ice, "
"and disruptions to ecosystems. In addition, human activities such as deforestation "
"and industrial pollution have accelerated these changes. Scientists emphasize the need "
"for immediate action to reduce greenhouse gas emissions and mitigate the impact on the environment."
)
# Define expected keywords for a good summary
expected_keywords = ["climate change", "global temperatures", "greenhouse gas emissions", "environment"]
summary = summarization_agent(test_text)
print("Generated Summary:\n", summary)
if all(keyword.lower() in summary.lower() for keyword in expected_keywords):
print("Test Passed: The summary covers key points.")
else:
print("Test Failed: The summary may be missing some key information.")
# Run the evaluation
evaluate_summarization()