Skip to content

Commit

Permalink
chore: fix formatting (#5326)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHYang authored Nov 12, 2024
1 parent 4a0fc78 commit ac0c93b
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 61 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches: [main, sessions]
pull_request:
paths:
- "**/*.py"
- "src/**"
- "tests/**"
- "tutorials/**"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import sys

sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.insert(1, os.path.join(sys.path[0], ".."))

import gradio as gr
from openinference.semconv.trace import SpanAttributes
from opentelemetry import trace
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

from router import run_autogen_agents
from utils.instrument import Framework, instrument

Expand Down
57 changes: 28 additions & 29 deletions examples/agent_framework_comparison/autogen_multi_agent/router.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import os
import sys

sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.insert(1, os.path.join(sys.path[0], ".."))

from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager, register_function
from autogen import AssistantAgent, GroupChat, GroupChatManager, UserProxyAgent, register_function
from calculator import calculator
from db.database import get_schema, get_table
from dotenv import load_dotenv
from openinference.semconv.trace import SpanAttributes
from opentelemetry import trace
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

from calculator import calculator
from sql_query import run_sql_query
from prompt_templates.router_template import SYSTEM_PROMPT as MANAGER_SYSTEM_PROMPT
from prompt_templates.sql_generator_template import SYSTEM_PROMPT as SQL_SYSTEM_PROMPT
from db.database import get_schema, get_table
from sql_query import run_sql_query

load_dotenv()

Expand All @@ -29,26 +28,21 @@ def run_autogen_agents(query, parent_context):
span.set_attribute(SpanAttributes.INPUT_MIME_TYPE, "application/json")
span.set_attribute(SpanAttributes.LLM_TOOLS, str(["calculator", "run_sql_query"]))

config_list = [
{
"model": "gpt-4o",
"api_key": os.environ["OPENAI_API_KEY"]
}
]
config_list = [{"model": "gpt-4o", "api_key": os.environ["OPENAI_API_KEY"]}]
llm_config = {"config_list": config_list, "cache_seed": 42}

calculator_agent = AssistantAgent(
name="Calculator",
description="Perform basic arithmetic operations (+, -, *, /) on two integers.",
system_message="You are a helpful assistant that can perform basic arithmetic operations "
"(+, -, *, /) on two integers.",
"(+, -, *, /) on two integers.",
llm_config=llm_config,
)
data_analyzer_agent = AssistantAgent(
name="Data_Analyzer",
description="Provide insights, trends, or analysis based on the data and prompt.",
system_message="You are a helpful assistant that can provide insights, trends, or analysis "
"based on the data and prompt.",
"based on the data and prompt.",
llm_config=llm_config,
)
sql_query_agent = AssistantAgent(
Expand All @@ -59,13 +53,13 @@ def run_autogen_agents(query, parent_context):
)

system_message = (
MANAGER_SYSTEM_PROMPT +
"First, identify and make all necessary agent calls based on the user prompt. " +
"Ensure that you gather and aggregate the results from these agent calls. " +
"Once all agent calls are completed and the final result is ready, return it in a single message. " +
"When the task is fully completed, ensure the final message contains the full result, "
"followed by 'TERMINATE' at the very end." +
"If the task is about finding any trends, use the SQL_Query Agent first, "
MANAGER_SYSTEM_PROMPT
+ "First, identify and make all necessary agent calls based on the user prompt. "
+ "Ensure that you gather and aggregate the results from these agent calls. "
+ "Once all agent calls are completed and the final result is ready, return it in a single message. "
+ "When the task is fully completed, ensure the final message contains the full result, "
"followed by 'TERMINATE' at the very end."
+ "If the task is about finding any trends, use the SQL_Query Agent first, "
"to retrieve the data for Data_Analyzer Agent."
)
manager_agent = AssistantAgent(
Expand All @@ -75,7 +69,8 @@ def run_autogen_agents(query, parent_context):
)
user_proxy_agent = UserProxyAgent(
name="User_Proxy",
is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
is_termination_msg=lambda msg: msg.get("content") is not None
and "TERMINATE" in msg["content"],
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
code_execution_config={
Expand All @@ -91,27 +86,31 @@ def run_autogen_agents(query, parent_context):
executor=user_proxy_agent,
name="Calculator_Tool",
description="A tool that can be used to perform basic arithmetic operations "
"(+, -, *, /) on two integers."
"(+, -, *, /) on two integers.",
)
register_function(
run_sql_query,
caller=sql_query_agent,
executor=user_proxy_agent,
name="SQL_Query_Executor_Tool",
description="A tool that can be used to run SQL query on the database."
description="A tool that can be used to run SQL query on the database.",
)

agents = [calculator_agent, data_analyzer_agent, sql_query_agent, manager_agent, user_proxy_agent]
agents = [
calculator_agent,
data_analyzer_agent,
sql_query_agent,
manager_agent,
user_proxy_agent,
]
groupchat = GroupChat(agents=agents, messages=[], max_round=15)
groupchat_manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)

result = user_proxy_agent.initiate_chat(
groupchat_manager,
message=query,
)
last_chat_message = result.chat_history[-1]['content']
last_chat_message = result.chat_history[-1]["content"]

span.set_attribute(
SpanAttributes.OUTPUT_VALUE, str(last_chat_message)
)
span.set_attribute(SpanAttributes.OUTPUT_VALUE, str(last_chat_message))
return last_chat_message
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
import sys
from typing import Annotated

sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.insert(1, os.path.join(sys.path[0], ".."))

from db.database import run_query
from openinference.semconv.trace import SpanAttributes
from opentelemetry import trace
from pydantic import BaseModel, Field

from db.database import run_query


class SQLQueryInput(BaseModel):
sql_query: Annotated[str, Field(description="The SQL query to retrieve dataset for data analysis.")]
sql_query: Annotated[
str, Field(description="The SQL query to retrieve dataset for data analysis.")
]


def run_sql_query(input: Annotated[SQLQueryInput, "Input to the SQL query executor."]) -> str:

def _sanitize_query(query):
query = query.strip()
if query.startswith("```") and query.endswith("```"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def _run(self, a: int, b: int, operator: Operator) -> int:
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("calculator_tool") as span:
span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "CHAIN")
span.set_attribute(SpanAttributes.INPUT_VALUE,
f"a={a}, b={b}, operator={operator}")
span.set_attribute(SpanAttributes.INPUT_VALUE, f"a={a}, b={b}, operator={operator}")

if operator == "+":
result = a + b
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import sys

sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.insert(1, os.path.join(sys.path[0], ".."))

import gradio as gr

from router import run_crewai
from utils.instrument import Framework, instrument

Expand Down
27 changes: 13 additions & 14 deletions examples/agent_framework_comparison/crewai_multi_agent/router.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import os
import sys

sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.insert(1, os.path.join(sys.path[0], ".."))

from calculator import CalculatorTool
from crewai import Agent, Crew, Process, Task
from db.database import get_schema, get_table
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

from calculator import CalculatorTool
from sql_query import SQLQueryTool
from prompt_templates.router_template import SYSTEM_PROMPT as MANAGER_SYSTEM_PROMPT
from prompt_templates.sql_generator_template import SYSTEM_PROMPT as SQL_SYSTEM_PROMPT
from db.database import get_schema, get_table
from sql_query import SQLQueryTool

load_dotenv()

Expand All @@ -26,20 +25,20 @@ def run_crewai(query):
role="Calculator",
goal="Perform basic arithmetic operations (+, -, *, /) on two integers.",
backstory="You are a helpful assistant that can perform basic arithmetic operations (+, -, *, /) "
"on two integers.",
"on two integers.",
tools=[calculator_tool],
allow_delegation=False,
verbose=True,
llm=llm
llm=llm,
)
data_analyzer_agent = Agent(
role="Data Analyzer",
goal="Provide insights, trends, or analysis based on the data and prompt.",
backstory="You are a helpful assistant that can provide insights, trends, or analysis based on "
"the data and prompt.",
"the data and prompt.",
allow_delegation=False,
verbose=True,
llm=llm
llm=llm,
)
sql_query_agent = Agent(
role="SQL Query",
Expand All @@ -48,7 +47,7 @@ def run_crewai(query):
tools=[sql_query_tool],
allow_delegation=False,
verbose=True,
llm=llm
llm=llm,
)

system_message = (
Expand All @@ -64,21 +63,21 @@ def run_crewai(query):
backstory=MANAGER_SYSTEM_PROMPT,
allow_delegation=True,
verbose=True,
llm=llm
llm=llm,
)

user_query_task = Task(
description=query,
expected_output="Once all agent calls are completed and the final result is ready, return it "
"in a single message.",
agent=manager_agent
"in a single message.",
agent=manager_agent,
)

crew = Crew(
agents=[calculator_agent, data_analyzer_agent, sql_query_agent],
tasks=[user_query_task],
process=Process.sequential,
manager_agent=manager_agent
manager_agent=manager_agent,
)
result = crew.kickoff()
return result.raw
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
import sys
from typing import Type

sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.insert(1, os.path.join(sys.path[0], ".."))

from crewai_tools import BaseTool
from db.database import run_query
from openinference.semconv.trace import SpanAttributes
from opentelemetry import trace
from pydantic import BaseModel, Field

from db.database import run_query


class SQLQueryInput(BaseModel):
"""Input for SQLQueryTool."""

sql_query: str = Field(
description="The SQL query to retrieve dataset for data analysis.")
sql_query: str = Field(description="The SQL query to retrieve dataset for data analysis.")


class SQLQueryTool(BaseTool):
Expand Down
3 changes: 2 additions & 1 deletion examples/agent_framework_comparison/utils/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from enum import Enum

from openinference.instrumentation.langchain import LangChainInstrumentor
from openinference.instrumentation.litellm import LiteLLMInstrumentor
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from openinference.instrumentation.openai import OpenAIInstrumentor
from openinference.instrumentation.litellm import LiteLLMInstrumentor

from phoenix.otel import register


Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ target-version = "py39"

[tool.ruff.lint.per-file-ignores]
"*.ipynb" = ["E402", "E501"]
"examples/**/*.py" = ["E501"]

[tool.ruff.lint]
select = ["E", "F", "W", "I", "NPY201"]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ commands =
[testenv:ruff]
description = Run ruff for formatting and linting
commands_pre =
uv tool install [email protected].0
uv tool install [email protected].3
commands =
uv tool run ruff format
uv tool run ruff check --fix

0 comments on commit ac0c93b

Please sign in to comment.