-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathsuperagent.py
82 lines (73 loc) · 2.82 KB
/
superagent.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
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
from langchain import hub
from langchain.chat_models import ChatOpenAI
import os
def document_qa_function(question):
return "$24B"
def tweet_generator(message):
answer = f"Tesla's Q2 revenue in 2023 was {message}. #Tesla #2023"
return answer
prompt = hub.pull("homanp/superagent")
os.environ["OPENAI_API_KEY"] = "Your_OpenAI_Key"
model = ChatOpenAI(model="gpt-3.5-turbo-0301")
runnable = prompt | model
output = runnable.invoke({
"tools": """Tools:
[{
"name": "Tesla Q2 2023 earnings",
"description": "useful for answering questions about Teslas Q2 2023 earnings report",
"function": "document_qa_function",
"input_schema" : {
"question": <str>
},
"output_schema": {
"answer": <str>,
}
}, {
"name": "Tweet generator",
"description": "useful for generating tweets",
"function": "tweeet_generator",
"input_schema" : {
"message": <str>
},
"output_schema": {
"tweet": <str>
}
}]""",
"output_format": """{
"workflow": "Generate Prompt and Replicate Image",
"steps": [
{
"name": "Tool name 1",
"function": "function_runner_1",
"input_schema": {
"query": str
},
"ouput_key": "result_1"
},
{
"name": "Function 2",
"function": "function_runner_2",
"input_schema": {
"input": "{result_1}"
},
"output_key": "result_2"
}
]
}""",
"input": "Write a tweet about the Q2 revenue"
})
import ast
data_dict = ast.literal_eval(output.content)
#call document_qa_function()
doc_qa_step = data_dict['steps'][0]
function_name = doc_qa_step['function']
arguments = doc_qa_step['input_schema']
function = globals()[function_name]
result = function(**arguments)
#call tweet_generator()
tweet_step = data_dict['steps'][1]
function_name = tweet_step['function']
arguments = {"message": result}
function = globals()[function_name]
result = function(**arguments)
print("New Tweet: ", result)