LangSmith can capture traces generated by AutoGen using OpenInference’s AutoGen instrumentation. This guide shows you how to automatically capture traces from your AutoGen multi-agent conversations and send them to LangSmith for monitoring and analysis.
Once configured, your AutoGen application will automatically send traces to LangSmith:
Copy
Ask AI
import autogenfrom openinference.instrumentation.autogen import AutogenInstrumentorfrom openinference.instrumentation.openai import OpenAIInstrumentorfrom langsmith.integrations.otel import configureimport osimport dotenv# Load environment variablesdotenv.load_dotenv(".env.local")# Configure LangSmith tracingconfigure(project_name="autogen-code-review")# Instrument AutoGen and OpenAIAutogenInstrumentor().instrument()OpenAIInstrumentor().instrument()# Configure your agentsconfig_list = [ { "model": "gpt-4", "api_key": os.getenv("OPENAI_API_KEY"), }]# Create a code reviewer agentcode_reviewer = autogen.AssistantAgent( name="code_reviewer", llm_config={"config_list": config_list}, system_message="""You are an expert code reviewer. Your role is to: 1. Review code for bugs, security issues, and best practices 2. Suggest improvements and optimizations 3. Provide constructive feedback Always be thorough but constructive in your reviews.""",)# Create a developer agentdeveloper = autogen.AssistantAgent( name="developer", llm_config={"config_list": config_list}, system_message="""You are a senior software developer. Your role is to: 1. Write clean, efficient code 2. Address feedback from code reviews 3. Explain your implementation decisions 4. Implement requested features and fixes""",)# Create a user proxy agentuser_proxy = autogen.UserProxyAgent( name="user_proxy", human_input_mode="NEVER", max_consecutive_auto_reply=8, is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"), code_execution_config={"work_dir": "workspace"}, llm_config={"config_list": config_list},)def run_code_review_session(task_description: str): """Run a multi-agent code review session.""" # Create a group chat with the agents groupchat = autogen.GroupChat( agents=[user_proxy, developer, code_reviewer], messages=[], max_round=10 ) # Create a group chat manager manager = autogen.GroupChatManager( groupchat=groupchat, llm_config={"config_list": config_list} ) # Start the conversation user_proxy.initiate_chat( manager, message=f""" Task: {task_description} Developer: Please implement the requested feature. Code Reviewer: Please review the implementation and provide feedback. Work together to create a high-quality solution. """ ) return "Code review session completed"# Example usageif __name__ == "__main__": task = """ Create a Python function that implements a binary search algorithm. The function should: - Take a sorted list and a target value as parameters - Return the index of the target if found, or -1 if not found - Include proper error handling and documentation """ result = run_code_review_session(task) print(f"Result: {result}")