Update app.py
#12
by
sskyd
- opened
app.py
CHANGED
@@ -7,11 +7,48 @@ from agent_build_sdk.sdk.agent import format_prompt
|
|
7 |
from prompts import DESC_PROMPT, VOTE_PROMPT
|
8 |
from agent_build_sdk.utils.logger import logger
|
9 |
|
10 |
-
|
11 |
import os
|
12 |
|
13 |
|
14 |
class SpyAgent(BasicAgent):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def perceive(self, req=AgentReq):
|
17 |
logger.info("spy perceive: {}".format(req))
|
|
|
7 |
from prompts import DESC_PROMPT, VOTE_PROMPT
|
8 |
from agent_build_sdk.utils.logger import logger
|
9 |
|
10 |
+
import openai
|
11 |
import os
|
12 |
|
13 |
|
14 |
class SpyAgent(BasicAgent):
|
15 |
+
def __init__(self, api_key: str):
|
16 |
+
super().__init__()
|
17 |
+
self.api_key = api_key
|
18 |
+
openai.api_key = self.api_key
|
19 |
+
|
20 |
+
def generate_response(self, prompt: str) -> str:
|
21 |
+
"""
|
22 |
+
Use GPT-3.5 to generate a response based on the provided prompt.
|
23 |
+
"""
|
24 |
+
try:
|
25 |
+
logger.info(f"Sending prompt to GPT-3.5: {prompt}")
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-3.5-turbo",
|
28 |
+
messages=[
|
29 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
30 |
+
{"role": "user", "content": prompt}
|
31 |
+
],
|
32 |
+
max_tokens=1000,
|
33 |
+
temperature=0.7
|
34 |
+
)
|
35 |
+
# Extract and return the content of the response
|
36 |
+
return response['choices'][0]['message']['content'].strip()
|
37 |
+
except Exception as e:
|
38 |
+
logger.error(f"Error generating response: {e}")
|
39 |
+
return "An error occurred while generating a response."
|
40 |
+
|
41 |
+
# Example usage
|
42 |
+
if __name__ == "__main__":
|
43 |
+
# Assuming you have set your OpenAI API key in an environment variable
|
44 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
45 |
+
if not api_key:
|
46 |
+
raise ValueError("OPENAI_API_KEY environment variable not set.")
|
47 |
+
|
48 |
+
agent = SpyAgent(api_key=api_key)
|
49 |
+
prompt = "Describe the role of AI in modern society."
|
50 |
+
response = agent.generate_response(prompt)
|
51 |
+
print(f"GPT-3.5 Response: {response}")
|
52 |
|
53 |
def perceive(self, req=AgentReq):
|
54 |
logger.info("spy perceive: {}".format(req))
|