-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathopen_ai_function_example.py
More file actions
184 lines (158 loc) · 7.44 KB
/
Copy pathopen_ai_function_example.py
File metadata and controls
184 lines (158 loc) · 7.44 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import asyncio
from workers.chat_workers import collect_history
from conductor.asyncio_client.adapters import ApiClient
from conductor.asyncio_client.adapters.models import ExtendedTaskDef
from conductor.asyncio_client.ai.orchestrator import AsyncAIOrchestrator
from conductor.asyncio_client.automator.task_handler import TaskHandler
from conductor.asyncio_client.configuration.configuration import Configuration
from conductor.asyncio_client.orkes.orkes_clients import OrkesClients
from conductor.asyncio_client.worker.worker_task import worker_task
from conductor.asyncio_client.workflow.conductor_workflow import AsyncConductorWorkflow
from conductor.asyncio_client.workflow.task.do_while_task import LoopTask
from conductor.asyncio_client.workflow.task.dynamic_task import DynamicTask
from conductor.asyncio_client.workflow.task.llm_tasks.llm_chat_complete import (
LlmChatComplete,
)
from conductor.asyncio_client.workflow.task.wait_task import WaitTask
from conductor.shared.http.enums import TaskResultStatus
from conductor.shared.workflow.enums import TimeoutPolicy
def start_workers(api_config):
task_handler = TaskHandler(
workers=[],
configuration=api_config,
scan_for_annotated_workers=True,
)
task_handler.start_processes()
return task_handler
@worker_task(task_definition_name="get_weather")
def get_weather(city: str) -> str:
return f"weather in {city} today is rainy"
@worker_task(task_definition_name="get_price_from_amazon")
def get_price_from_amazon(product: str) -> float:
return 42.42
async def main():
llm_provider = "openai"
chat_complete_model = "gpt-5"
api_config = Configuration()
api_config.apply_logging_config()
async with ApiClient(api_config) as api_client:
clients = OrkesClients(configuration=api_config, api_client=api_client)
workflow_executor = clients.get_workflow_executor()
workflow_client = clients.get_workflow_client()
task_client = clients.get_task_client()
metadata_client = clients.get_metadata_client()
task_handler = start_workers(api_config=api_config)
# register our two tasks
await metadata_client.register_task_def(
task_def=ExtendedTaskDef(
name="get_weather", timeout_seconds=3600, total_timeout_seconds=3600
)
)
await metadata_client.register_task_def(
task_def=ExtendedTaskDef(
name="get_price_from_amazon",
timeout_seconds=3600,
total_timeout_seconds=3600,
)
)
# Define and associate prompt with the AI integration
prompt_name = "chat_function_instructions"
prompt_text = """
You are a helpful assistant that can answer questions using tools provided.
You have the following tools specified as functions in python:
1. get_weather(city:str) -> str (useful to get weather for a city input is the city name or zipcode)
2. get_price_from_amazon(str: item) -> float (useful to get the price of an item from amazon)
When asked a question, you can use one of these functions to answer the question if required.
If you have to call these functions, respond with a python code that will call this function.
When you have to call a function return in the following valid JSON format that can be parsed using json util:
{
"type": "function",
"function": "ACTUAL_PYTHON_FUNCTION_NAME_TO_CALL_WITHOUT_PARAMETERS"
"function_parameters": "PARAMETERS FOR THE FUNCTION as a JSON map with key as parameter name and value as parameter value"
}
"""
orchestrator = AsyncAIOrchestrator(
api_configuration=api_config, api_client=api_client
)
await orchestrator.add_prompt_template(
prompt_name, prompt_text, "chat instructions"
)
# associate the prompts
await orchestrator.associate_prompt_template(
prompt_name, llm_provider, [chat_complete_model]
)
wf = AsyncConductorWorkflow(
name="my_function_chatbot", version=1, executor=workflow_executor
)
user_input = WaitTask(task_ref_name="get_user_input")
collect_history_task = collect_history(
task_ref_name="collect_history_ref",
user_input=user_input.output("question"),
history="${chat_complete_ref.input.messages}",
assistant_response="${chat_complete_ref.output.result}",
)
chat_complete = LlmChatComplete(
task_ref_name="chat_complete_ref",
llm_provider=llm_provider,
model=chat_complete_model,
instructions_template=prompt_name,
messages=collect_history_task,
)
function_call = DynamicTask(
task_reference_name="fn_call_ref",
dynamic_task=chat_complete.output("function"),
)
function_call.input_parameters["inputs"] = chat_complete.output(
"function_parameters"
)
function_call.input_parameters["dynamicTaskInputParam"] = "inputs"
# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
loop_tasks = [user_input, collect_history_task, chat_complete, function_call]
# ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
chat_loop = LoopTask(task_ref_name="loop", iterations=3, tasks=loop_tasks)
wf >> chat_loop
# let's make sure we don't run it for more than 2 minutes -- avoid runaway loops
wf.timeout_seconds(120).timeout_policy(
timeout_policy=TimeoutPolicy.TIME_OUT_WORKFLOW
)
message = """
AI Function call example.
This chatbot is programmed to handle two types of queries:
1. Get the weather for a location
2. Get the price of an item
"""
print(message)
workflow_run = await wf.execute(
wait_until_task_ref=user_input.task_reference_name, wait_for_seconds=1
)
workflow_id = workflow_run.workflow_id
while workflow_run.status == "RUNNING":
if (
workflow_run.current_task.workflow_task.task_reference_name
== user_input.task_reference_name
):
function_call_task = workflow_run.get_task(
task_reference_name=function_call.task_reference_name
)
if function_call_task is not None:
assistant = function_call_task.output_data["result"]
print(f"assistant: {assistant}")
if (
workflow_run.current_task.workflow_task.task_reference_name
== user_input.task_reference_name
):
question = input("Question: >> ")
await task_client.update_task_sync(
workflow_id=workflow_id,
task_ref_name=user_input.task_reference_name,
status=TaskResultStatus.COMPLETED,
request_body={"question": question},
)
await asyncio.sleep(0.5)
workflow_run = await workflow_client.get_workflow(
workflow_id=workflow_id, include_tasks=True
)
print(f"{workflow_run.output}")
task_handler.stop_processes()
if __name__ == "__main__":
asyncio.run(main())