-
Notifications
You must be signed in to change notification settings - Fork 18
/
ra_final.py
49 lines (37 loc) · 1.39 KB
/
ra_final.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
from openai_module import generate_text_with_conversation
from prompts import react_system_prompt
from sample_functions import get_weather
from json_helpers import extract_json
#Available actions are:
available_actions = {
"get_weather": get_weather
}
prompt = """
what is digital marketing?"""
messages = [
{"role": "system", "content": react_system_prompt},
{"role": "user", "content": prompt},
]
turn_count = 1
max_turns = 5
while turn_count < max_turns:
print (f"Loop: {turn_count}")
print("----------------------")
turn_count += 1
response = generate_text_with_conversation(messages, model="gpt-4")
print(response)
json_function = extract_json(response)
if json_function:
function_name = json_function[0]['function_name']
function_parms = json_function[0]['function_parms']
if function_name not in available_actions:
raise Exception(f"Unknown action: {function_name}: {function_parms}")
print(f" -- running {function_name} {function_parms}")
action_function = available_actions[function_name]
#call the function
result = action_function(**function_parms)
function_result_message = f"Action_Response: {result}"
messages.append({"role": "user", "content": function_result_message})
print(function_result_message)
else:
break