|
| 1 | +import time |
| 2 | +import uuid |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from conductor.client.ai.orchestrator import AIOrchestrator |
| 6 | +from conductor.client.automator.task_handler import TaskHandler |
| 7 | +from conductor.client.configuration.configuration import Configuration |
| 8 | +from conductor.client.orkes_clients import OrkesClients |
| 9 | +from conductor.client.worker.worker_task import worker_task |
| 10 | +from conductor.client.workflow.conductor_workflow import ConductorWorkflow |
| 11 | +from conductor.client.workflow.task.do_while_task import LoopTask |
| 12 | +from conductor.client.workflow.task.llm_tasks.llm_chat_complete import LlmChatComplete, ChatMessage |
| 13 | +from conductor.client.workflow.task.set_variable_task import SetVariableTask |
| 14 | +from conductor.client.workflow.task.simple_task import SimpleTask |
| 15 | +from conductor.client.workflow.task.switch_task import SwitchTask |
| 16 | +from conductor.client.workflow.task.timeout_policy import TimeoutPolicy |
| 17 | + |
| 18 | + |
| 19 | +def main(): |
| 20 | + agent1_provider = 'openai_v1' |
| 21 | + agent1_model = 'gpt-4' |
| 22 | + |
| 23 | + agent1_provider = 'mistral' |
| 24 | + agent1_model = 'mistral-large-latest' |
| 25 | + |
| 26 | + agent2_provider = 'anthropic_cloud' |
| 27 | + agent2_model = 'claude-3-sonnet-20240229' |
| 28 | + # anthropic_model = 'claude-3-opus-20240229' |
| 29 | + |
| 30 | + moderator_provider = 'cohere_saas' |
| 31 | + moderator_model = 'command-r' |
| 32 | + |
| 33 | + mistral = 'mistral' |
| 34 | + mistral_model = 'mistral-large-latest' |
| 35 | + |
| 36 | + api_config = Configuration() |
| 37 | + |
| 38 | + clients = OrkesClients(configuration=api_config) |
| 39 | + workflow_executor = clients.get_workflow_executor() |
| 40 | + workflow_client = clients.get_workflow_client() |
| 41 | + |
| 42 | + moderator = 'moderator' |
| 43 | + moderator_text = """You are very good at moderating the debates and discussions. In this discussion, there are 2 panelists, ${ua1} and ${ua2}. |
| 44 | + As a moderator, you summarize the discussion so far, pick one of the panelist ${ua1} or ${ua2} and ask them a relevant question to continue the discussion. |
| 45 | + You are also an expert in formatting the results into structured json format. You only output a valid JSON as a response. |
| 46 | + You answer in RFC8259 compliant |
| 47 | + JSON format ONLY with two fields result and user. You can effectively manage a hot discussion while keeping it |
| 48 | + quite civil and also at the same time continue the discussion forward encouraging participants and their views. |
| 49 | + Your answer MUST be in a JSON dictionary with keys "result" and "user". Before answer, check the output for correctness of the JSON format. |
| 50 | + The values MUST not have new lines or special characters that are not escaped. The JSON must be RFC8259 compliant. |
| 51 | + |
| 52 | + You produce the output in the following JSON keys: |
| 53 | +
|
| 54 | + { |
| 55 | + "result": ACTUAL_MESSAGE |
| 56 | + "user": USER_WHO_SOULD_RESPOND_NEXT --> One of ${ua1} or ${ua2} |
| 57 | + } |
| 58 | +
|
| 59 | + "result" should summarize the conversation so far and add the last message in the conversation. |
| 60 | + "user" should be the one who should respond next. |
| 61 | + You be fair in giving chance to all participants, alternating between ${ua1} and ${ua2}. |
| 62 | + the last person to talk was ${last_user} |
| 63 | + Do not repeat what you have said before and do not summarize the discussion each time, |
| 64 | + just use first person voice to ask questions to move discussion forward. |
| 65 | + Do not use filler sentences like 'in this discussion....' |
| 66 | + JSON: |
| 67 | + |
| 68 | + """ |
| 69 | + |
| 70 | + agent1 = 'agent_1' |
| 71 | + agent1_text = """ |
| 72 | + You are ${ua1} and you reason and think like ${ua1}. Your language reflects your persona. |
| 73 | + You are very good at analysis of the content and coming up with insights and questions on the subject and the context. |
| 74 | + You are in a panel with other participants discussing a specific event/topic as set in the context. |
| 75 | + You avoid any repetitive argument, discussion that you have already talked about. |
| 76 | + Here is the context on the conversation, add a follow up with your insights and questions to the conversation: |
| 77 | + Do not mention that you are an AI model. |
| 78 | + ${context} |
| 79 | + |
| 80 | + You answer in a very clear way, do not add any preamble to the response: |
| 81 | + """ |
| 82 | + |
| 83 | + agent2 = 'agent_2' |
| 84 | + agent2_text = """ |
| 85 | + You are ${ua2} and you reason and think like ${ua2}. Your language reflects your persona. |
| 86 | + You are very good at continuing the conversation with more insightful question. |
| 87 | + You are in a panel with other participants discussing a specific event/topic as set in the context. |
| 88 | + You bring in your contrarian views to the conversation and always challenge the norms. |
| 89 | + You avoid any repetitive argument, discussion that you have already talked about. |
| 90 | + Your responses are times extreme and a bit hyperbolic. |
| 91 | + When given the history of conversation, you ask a meaningful followup question that continues to conversation |
| 92 | + and dives deeper into the topic. |
| 93 | + Do not mention that you are an AI model. |
| 94 | + Here is the context on the conversation: |
| 95 | + ${context} |
| 96 | + |
| 97 | + You answer in a very clear way, do not add any preamble to the response: |
| 98 | + """ |
| 99 | + |
| 100 | + orchestrator = AIOrchestrator(api_configuration=api_config) |
| 101 | + |
| 102 | + orchestrator.add_prompt_template(moderator, moderator_text, 'moderator instructions') |
| 103 | + orchestrator.associate_prompt_template(moderator, moderator_provider, [moderator_model]) |
| 104 | + |
| 105 | + orchestrator.add_prompt_template(agent1, agent1_text, 'agent1 instructions') |
| 106 | + orchestrator.associate_prompt_template(agent1, agent1_provider, [agent1_model]) |
| 107 | + |
| 108 | + orchestrator.add_prompt_template(agent2, agent2_text, 'agent2 instructions') |
| 109 | + orchestrator.associate_prompt_template(agent2, agent2_provider, [agent2_model]) |
| 110 | + |
| 111 | + get_context = SimpleTask(task_reference_name='get_document', task_def_name='GET_DOCUMENT') |
| 112 | + get_context.input_parameter('url','${workflow.input.url}') |
| 113 | + |
| 114 | + wf_input = {'ua1': 'donald trump', 'ua2': 'joe biden', 'last_user': '${workflow.variables.last_user}', |
| 115 | + 'url': 'https://www.foxnews.com/media/billionaire-mark-cuban-dodges-question-asking-pays-fair-share-taxes-pay-owe'} |
| 116 | + |
| 117 | + template_vars = { |
| 118 | + 'context': get_context.output('result'), |
| 119 | + 'ua1': '${workflow.input.ua1}', |
| 120 | + 'ua2': '${workflow.input.ua2}', |
| 121 | + } |
| 122 | + |
| 123 | + max_tokens = 500 |
| 124 | + moderator_task = LlmChatComplete(task_ref_name='moderator_ref', |
| 125 | + max_tokens=2000, |
| 126 | + llm_provider=moderator_provider, model=moderator_model, |
| 127 | + instructions_template=moderator, |
| 128 | + messages='${workflow.variables.history}', |
| 129 | + template_variables={ |
| 130 | + 'ua1': '${workflow.input.ua1}', |
| 131 | + 'ua2': '${workflow.input.ua2}', |
| 132 | + 'last_user': '${workflow.variables.last_user}' |
| 133 | + }) |
| 134 | + |
| 135 | + agent1_task = LlmChatComplete(task_ref_name='agent1_ref', |
| 136 | + max_tokens=max_tokens, |
| 137 | + llm_provider=agent1_provider, model=agent1_model, |
| 138 | + instructions_template=agent1, |
| 139 | + messages=[ChatMessage(role='user', message=moderator_task.output('result'))], |
| 140 | + template_variables=template_vars) |
| 141 | + |
| 142 | + set_variable1 = (SetVariableTask(task_ref_name='task_ref_name1') |
| 143 | + .input_parameter('history', |
| 144 | + [ |
| 145 | + ChatMessage(role='assistant', message=moderator_task.output('result')), |
| 146 | + ChatMessage(role='user', |
| 147 | + message='[' + '${workflow.input.ua1}] ' + f'{agent1_task.output("result")}') |
| 148 | + ]) |
| 149 | + .input_parameter('_merge', True) |
| 150 | + .input_parameter('last_user', "${workflow.input.ua1}")) |
| 151 | + |
| 152 | + agent2_task = LlmChatComplete(task_ref_name='agent2_ref', |
| 153 | + max_tokens=max_tokens, |
| 154 | + llm_provider=agent2_provider, model=agent2_model, |
| 155 | + instructions_template=agent2, |
| 156 | + messages=[ChatMessage(role='user', message=moderator_task.output('result'))], |
| 157 | + template_variables=template_vars) |
| 158 | + |
| 159 | + set_variable2 = (SetVariableTask(task_ref_name='task_ref_name2') |
| 160 | + .input_parameter('history', [ |
| 161 | + ChatMessage(role='assistant', message=moderator_task.output('result')), |
| 162 | + ChatMessage(role='user', message='[' + '${workflow.input.ua2}] ' + f'{agent2_task.output("result")}') |
| 163 | + ]) |
| 164 | + .input_parameter('_merge', True) |
| 165 | + .input_parameter('last_user', "${workflow.input.ua2}")) |
| 166 | + |
| 167 | + init = SetVariableTask(task_ref_name='init_ref') |
| 168 | + init.input_parameter('history', |
| 169 | + [ChatMessage(role='user', |
| 170 | + message="""analyze the following context: |
| 171 | + BEGIN |
| 172 | + ${get_document.output.result} |
| 173 | + END """)] |
| 174 | + ) |
| 175 | + init.input_parameter('last_user', '') |
| 176 | + |
| 177 | + wf = ConductorWorkflow(name='multiparty_chat_tmp', version=1, executor=workflow_executor) |
| 178 | + |
| 179 | + script = """ |
| 180 | + (function(){ |
| 181 | + if ($.user == $.ua1) return 'ua1'; |
| 182 | + if ($.user == $.ua2) return 'ua2'; |
| 183 | + return 'ua1'; |
| 184 | + })(); |
| 185 | + """ |
| 186 | + next_up = SwitchTask(task_ref_name='next_up_ref', case_expression=script, use_javascript=True) |
| 187 | + next_up.switch_case('ua1', [agent1_task, set_variable1]) |
| 188 | + next_up.switch_case('ua2', [agent2_task, set_variable2]) |
| 189 | + next_up.input_parameter('user', moderator_task.output('user')) |
| 190 | + next_up.input_parameter('ua1', '${workflow.input.ua1}') |
| 191 | + next_up.input_parameter('ua2', '${workflow.input.ua2}') |
| 192 | + |
| 193 | + loop_tasks = [moderator_task, next_up] |
| 194 | + chat_loop = LoopTask(task_ref_name='loop', iterations=6, tasks=loop_tasks) |
| 195 | + wf >> get_context >> init >> chat_loop |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + wf.timeout_seconds(1200).timeout_policy(timeout_policy=TimeoutPolicy.TIME_OUT_WORKFLOW) |
| 200 | + wf.register(overwrite=True) |
| 201 | + |
| 202 | + result = wf.execute(wait_until_task_ref=agent1_task.task_reference_name, wait_for_seconds=1, |
| 203 | + workflow_input=wf_input) |
| 204 | + |
| 205 | + result = workflow_client.get_workflow_status(result.workflow_id, include_output=True, include_variables=True) |
| 206 | + print(f'started workflow {api_config.ui_host}/{result.workflow_id}') |
| 207 | + while result.is_running(): |
| 208 | + time.sleep(10) # wait for 10 seconds LLMs are slow! |
| 209 | + result = workflow_client.get_workflow_status(result.workflow_id, include_output=True, include_variables=True) |
| 210 | + op = result.variables['history'] |
| 211 | + if len(op) > 1: |
| 212 | + print('=======================================') |
| 213 | + print(f'{op[len(op) - 1]["message"]}') |
| 214 | + print('\n') |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == '__main__': |
| 218 | + main() |
0 commit comments