Create app-with-custom-models.py
Browse files- app-with-custom-models.py +153 -0
app-with-custom-models.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import gradio as gr
|
3 |
+
from autogen.runtime_logging import start, stop
|
4 |
+
from autogen_agentchat.agents import AssistantAgent
|
5 |
+
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
|
6 |
+
from autogen_agentchat.teams import RoundRobinGroupChat
|
7 |
+
from autogen_ext.models.openai import OpenAIChatCompletionClient
|
8 |
+
from autogen_agentchat.base import TaskResult
|
9 |
+
|
10 |
+
# Configuration
|
11 |
+
LOG_FILE = "team_runtime.log"
|
12 |
+
|
13 |
+
|
14 |
+
config_list_primary = [
|
15 |
+
{"model": "lm-broca", "api_type": "openai", "max_tokens": 4096, "api_key": "sk-", "base_url": "<base-url>", "tags": ["lm-broca", "openai"]},
|
16 |
+
]
|
17 |
+
|
18 |
+
config_list_critic = [
|
19 |
+
{"model": "groq-mixtral-8x7b-32768", "api_type": "openai", "max_tokens": 16192, "api_key": "sk-", "base_url": "<base-url>", "tags": ["groq-mixtral-8x7b-32768", "openai"]},
|
20 |
+
]
|
21 |
+
|
22 |
+
llm_config_primary = {
|
23 |
+
"config_list": config_list_primary,
|
24 |
+
}
|
25 |
+
|
26 |
+
llm_config_critic = {
|
27 |
+
"config_list": config_list_critic,
|
28 |
+
}
|
29 |
+
|
30 |
+
#def create_llm_config_critic():
|
31 |
+
# return {
|
32 |
+
# "model": "groq-mixtral-8x7b-32768",
|
33 |
+
# "api_key": "sk-BhYjxpcKH_4w4H9jduTVwA",
|
34 |
+
# "base_url": "https://litellm.j78.org/v1",
|
35 |
+
# "cache_seed": None
|
36 |
+
# }
|
37 |
+
|
38 |
+
|
39 |
+
# Create the team with primary and critic agents
|
40 |
+
def create_team(llm_config_primary, primary_system_message, critic_system_message):
|
41 |
+
model_client = OpenAIChatCompletionClient(**llm_config)
|
42 |
+
|
43 |
+
primary_agent = AssistantAgent(
|
44 |
+
"primary",
|
45 |
+
llm_config_primary={"config_list": config_list_primary})
|
46 |
+
system_message=primary_system_message,
|
47 |
+
)
|
48 |
+
|
49 |
+
critic_agent = AssistantAgent(
|
50 |
+
"critic",
|
51 |
+
llm_config_critic={"config_list": config_list_critic})
|
52 |
+
system_message=critic_system_message
|
53 |
+
)
|
54 |
+
|
55 |
+
# Set termination conditions (10-message cap OR "APPROVE" detected)
|
56 |
+
max_message_termination = MaxMessageTermination(max_messages=10)
|
57 |
+
text_termination = TextMentionTermination("APPROVE")
|
58 |
+
combined_termination = max_message_termination | text_termination
|
59 |
+
|
60 |
+
team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=combined_termination)
|
61 |
+
return team, model_client
|
62 |
+
|
63 |
+
# Function to stream the task through the workflow
|
64 |
+
async def async_stream_task(task_message, api_key, primary_system_message, critic_system_message, documentation_system_message):
|
65 |
+
# Start logging
|
66 |
+
logging_session_id = start(logger_type="file", config={"filename": LOG_FILE})
|
67 |
+
print(f"Logging session ID: {logging_session_id}")
|
68 |
+
|
69 |
+
llm_config_primary={"config_list": config_list_primary})
|
70 |
+
llm_config={"config_list": config_list_primary})
|
71 |
+
team, model_client = create_team(llm_config, primary_system_message, critic_system_message)
|
72 |
+
documentation_triggered = False # Track if documentation agent was triggered
|
73 |
+
final_output = None # Store the final approved output
|
74 |
+
|
75 |
+
try:
|
76 |
+
async for message in team.run_stream(task=task_message):
|
77 |
+
if hasattr(message, "source") and hasattr(message, "content"):
|
78 |
+
# Handle critic's approval
|
79 |
+
if message.source == "critic" and "APPROVE" in message.content:
|
80 |
+
print("Critic approved the response. Handing off to Documentation Agent...")
|
81 |
+
documentation_triggered = True
|
82 |
+
final_output = task_message # Capture the final approved output
|
83 |
+
break
|
84 |
+
yield message.source, message.content
|
85 |
+
|
86 |
+
# Trigger Documentation Agent if approved
|
87 |
+
if documentation_triggered and final_output:
|
88 |
+
documentation_agent = AssistantAgent(
|
89 |
+
"documentation",
|
90 |
+
model_client=model_client,
|
91 |
+
system_message=documentation_system_message,
|
92 |
+
)
|
93 |
+
doc_task = f"Generate a '--help' message for the following code:\n\n{final_output}"
|
94 |
+
async for doc_message in documentation_agent.run_stream(task=doc_task):
|
95 |
+
if isinstance(doc_message, TaskResult):
|
96 |
+
# Extract messages from TaskResult
|
97 |
+
for msg in doc_message.messages:
|
98 |
+
yield msg.source, msg.content
|
99 |
+
else:
|
100 |
+
yield doc_message.source, doc_message.content
|
101 |
+
|
102 |
+
finally:
|
103 |
+
# Stop logging
|
104 |
+
stop()
|
105 |
+
|
106 |
+
# Gradio interface function
|
107 |
+
async def chat_interface(api_key, primary_system_message, critic_system_message, documentation_system_message, task_message):
|
108 |
+
primary_messages = []
|
109 |
+
critic_messages = []
|
110 |
+
documentation_messages = []
|
111 |
+
|
112 |
+
# Append new messages while streaming
|
113 |
+
async for source, output in async_stream_task(task_message, api_key, primary_system_message, critic_system_message, documentation_system_message):
|
114 |
+
if source == "primary":
|
115 |
+
primary_messages.append(output)
|
116 |
+
elif source == "critic":
|
117 |
+
critic_messages.append(output)
|
118 |
+
elif source == "documentation":
|
119 |
+
documentation_messages.append(output)
|
120 |
+
|
121 |
+
# Return all outputs
|
122 |
+
yield (
|
123 |
+
"\n".join(primary_messages),
|
124 |
+
"\n".join(critic_messages),
|
125 |
+
"\n".join(documentation_messages),
|
126 |
+
)
|
127 |
+
|
128 |
+
# Gradio interface
|
129 |
+
iface = gr.Interface(
|
130 |
+
fn=chat_interface,
|
131 |
+
inputs=[
|
132 |
+
gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API Key"),
|
133 |
+
gr.Textbox(label="Primary Agent System Message", placeholder="Enter the system message for the primary agent", value="You are a creative assistant focused on producing high-quality code."),
|
134 |
+
gr.Textbox(label="Critic Agent System Message", placeholder="Enter the system message for the critic agent (requires APPROVAL tag!)", value="You are a critic assistant highly skilled in evaluating the quality of a given code or response. Provide constructive feedback and respond with 'APPROVE' once the feedback is addressed. Do not produce any content or code yourself, only provide feedback!"),
|
135 |
+
gr.Textbox(label="Documentation Agent System Message", placeholder="Enter the system message for the documentation agent", value="You are a documentation assistant. Write a short and concise '--help' message for the provided code."),
|
136 |
+
gr.Textbox(label="Task Message", placeholder="Code a random password generator using python."),
|
137 |
+
],
|
138 |
+
outputs=[
|
139 |
+
gr.Textbox(label="The Primary Assistant Messages"),
|
140 |
+
gr.Textbox(label="The Critics Assistant Messages"),
|
141 |
+
gr.Textbox(label="The Documentation Assistant Message"),
|
142 |
+
],
|
143 |
+
title="Team Workflow with Documentation Agent and Hard Cap",
|
144 |
+
description="""Collaborative workflow between Primary, Critic, and Documentation agents.
|
145 |
+
1. The user can send a prompt to the primary agent.
|
146 |
+
2. The response will then be evaluated by the critic, which either sends feedback back to the primary agent or gives the APPROVAL sign.
|
147 |
+
3. If the APPROVAL sign is given, the documentation agent is asked to write a short documentation for the code (that has been approved by the critic and generated by the priamry agent.
|
148 |
+
4. (Note: There is a hard cap of 10 messages for the critic to approve the output of the primary agent. If it fails to do so the workflow is interrupted to prevent long loops)"""
|
149 |
+
)
|
150 |
+
|
151 |
+
# Launch the app
|
152 |
+
if __name__ == "__main__":
|
153 |
+
iface.launch(share=True)
|