CodeMixt / agency.py
acecalisto3's picture
Create agency.py
2883d37 verified
from huggingface_hub import InferenceClient
import gradio as gr
import random
import subprocess
from typing import List, Dict, Any
API_URL = "https://api-inference.huggingface.co/models/"
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
def format_prompt(message: str, history: List[tuple], agent_roles: List[str]) -> str:
"""Formats the prompt with the selected agent roles and conversation history."""
prompt = f"""You are an expert agent cluster, consisting of {', '.join(agent_roles)}. Respond with complete program coding to client requests. Using available tools, please explain the researched information. Please don't answer based solely on what you already know. Always perform a search before providing a response. In special cases, such as when the user specifies a page to read, there's no need to search. Please read the provided page and answer the user's question accordingly. If you find that there's not much information just by looking at the search results page, consider these two options and try them out:
Try clicking on the links of the search results to access and read the content of each page.
Change your search query and perform a new search.
Users are extremely busy and not as free as you are. Therefore, to save the user's effort, please provide direct answers.
BAD ANSWER EXAMPLE
Please refer to these pages.
You can write code referring these pages.
Following page will be helpful.
GOOD ANSWER EXAMPLE
This is the complete code: -- complete code here --
The answer to your question is -- answer here --
Please make sure to list the URLs of the pages you referenced at the end of your answer. (This will allow users to verify your response.)
Please make sure to answer in the language used by the user. If the user asks in Japanese, please answer in Japanese. If the user asks in Spanish, please answer in Spanish. But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
"""
for user_prompt, bot_response in history:
prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
prompt += f"[INST] {message} [/INST]"
return prompt
def generate(prompt: str, history: List[tuple], agent_roles: List[str], temperature: float = 0.9, max_new_tokens: int = 2048, top_p: float = 0.95, repetition_penalty: float = 1.0) -> str:
"""Generates a response using the selected agent roles and parameters."""
temperature = max(0.01, float(temperature))
top_p = float(top_p)
generate_kwargs = {
"temperature": temperature,
"max_new_tokens": max_new_tokens,
"top_p": top_p,
"repetition_penalty": repetition_penalty,
"do_sample": True,
"seed": random.randint(0, 10**7),
}
formatted_prompt = format_prompt(prompt, history, agent_roles)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
yield output
def run_code(code: str) -> str:
"""Executes the provided code and returns the output."""
try:
output = subprocess.check_output(['python', '-c', code], stderr=subprocess.STDOUT, universal_newlines=True)
return output
except subprocess.CalledProcessError as e:
return f"Error: {e.output}"
def chat_interface(message: str, history: List[tuple], agent_cluster: Dict[str, bool], temperature: float = 0.9, max_new_tokens: int = 2048, top_p: float = 0.95, repetition_penalty: float = 1.0) -> tuple:
"""Handles user input and generates responses."""
if message.startswith("```python"):
# User entered code, execute it
code = message[9:-3]
output = run_code(code)
return message, output
else:
# User entered a normal message, generate a response
active_agents = [agent for agent, is_active in agent_cluster.items() if is_active]
response = generate(message, history, active_agents, temperature, max_new_tokens, top_p, repetition_penalty)
return message, response
# Define the available agent roles
agent_roles = {
"Web Developer": {"description": "A master of front-end and back-end web development.", "active": False},
"Prompt Engineer": {"description": "An expert in crafting effective prompts for AI models.", "active": False},
"Python Code Developer": {"description": "A skilled Python programmer who can write clean and efficient code.", "active": False},
"Hugging Face Hub Expert": {"description": "A specialist in navigating and utilizing the Hugging Face Hub.", "active": False},
"AI-Powered Code Assistant": {"description": "An AI assistant that can help with coding tasks and provide code snippets.", "active": False},
}
def toggle_agent(agent_name: str) -> str:
"""Toggles the active state of an agent."""
agent_roles[agent_name]["active"] = not agent_roles[agent_name]["active"]
return f"{agent_name} is now {'active' if agent_roles[agent_name]['active'] else 'inactive'}"
def get_agent_cluster() -> Dict[str, bool]:
"""Returns a dictionary of active agents."""
return {agent: agent_roles[agent]["active"] for agent in agent_roles}
with gr.Blocks(theme='ParityError/Interstellar') as demo:
with gr.Row():
for agent_name, agent_data in agent_roles.items():
gr.Button(agent_name, variant="secondary").click(toggle_agent, inputs=[gr.Button], outputs=[gr.Textbox])
gr.Textbox(agent_data["description"], interactive=False)
with gr.Row():
gr.ChatInterface(
chat_interface,
additional_inputs=[
gr.Slider(
label="Temperature",
value=0.9,
minimum=0.0,
maximum=1.0,
step=0.05,
interactive=True,
info="Higher values generate more diverse outputs",
),
gr.Slider(
label="Maximum New Tokens",
value=2048,
minimum=64,
maximum=4096,
step=64,
interactive=True,
info="The maximum number of new tokens",
),
gr.Slider(
label="Top-p (Nucleus Sampling)",
value=0.90,
minimum=0.0,
maximum=1,
step=0.05,
interactive=True,
info="Higher values sample more low-probability tokens",
),
gr.Slider(
label="Repetition Penalty",
value=1.2,
minimum=1.0,
maximum=2.0,
step=0.05,
interactive=True,
info="Penalize repeated tokens",
)
],
inputs=[gr.Textbox, gr.Chatbot, get_agent_cluster],
)
demo.queue().launch(debug=True)