-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Draft : Add agent astronomy shop and astronomy shop MCP server #3148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fali007
wants to merge
15
commits into
open-telemetry:main
Choose a base branch
from
fali007:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a2b98b5
[3140] Add agent astronomy shop and astronomy shop MCP server
fali007 f642af2
Add Dockerfile
fali007 803f8d1
Add package versions to requirements.txt
fali007 9f7dbc6
Fix requirements.txt
fali007 2eaa316
Add licence
fali007 0084067
Fix vcr recording bug
fali007 9cd2754
Add separate components for agent, MCP and ChatBot related changes
fali007 2700d28
Merge branch 'main' into main
fali007 87b319a
Fix yaml lint error
fali007 ab1b7bf
Merge branch 'main' into main
fali007 b65a252
Merge branch 'main' into main
fali007 4b0bf54
Added changes requested in PR
fali007 34c79b4
Removed trailing space
fali007 a20a658
Removed additional spaces before line start
fali007 bd8ff14
Merge branch 'main' into main
fali007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| FROM python:3.11-slim | ||
|
|
||
| ENV PYTHONDONTWRITEBYTECODE=1 \ | ||
| PYTHONUNBUFFERED=1 | ||
| WORKDIR /app | ||
|
|
||
| RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
| build-essential \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| COPY src/agent/requirements.txt . | ||
|
|
||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| COPY src/agent/src src | ||
| COPY src/agent/run.py . | ||
|
|
||
| EXPOSE ${AGENT_PORT} | ||
|
|
||
| CMD ["python", "run.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| requests==2.32.5 | ||
| traceloop-sdk==0.47.5 | ||
| dotenv==0.9.9 | ||
| httpx==0.28.1 | ||
| langchain_openai==1.0.1 | ||
| langchain==1.0.5 | ||
| langgraph-prebuilt==1.0.5 | ||
| mcp==1.22.0 | ||
| fastmcp>=2.13.1 | ||
| langchain_mcp_adapters==0.1.14 | ||
| langchain_community==0.4.1 | ||
| fastapi==0.120.4 | ||
| opentelemetry-api==1.38.0 | ||
| opentelemetry-sdk==1.38.0 | ||
| opentelemetry-semantic-conventions==0.59b0 | ||
| opentelemetry-instrumentation-langchain<0.53.0 | ||
| gradio==6.0.1 | ||
| vcrpy==7.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| import asyncio | ||
| import logging | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
| from src.agents.agents import Agent | ||
| from traceloop.sdk import Traceloop | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| load_dotenv() | ||
|
|
||
| Traceloop.init( | ||
| app_name="AstronomyShopAgent", | ||
| api_endpoint=os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "localhost:4317"), | ||
| ) | ||
|
|
||
|
|
||
| async def start_servers(): | ||
| """Run the LangGraph Agent server""" | ||
| tasks = [] | ||
| agent = Agent() | ||
| tasks.append(agent.launch()) | ||
| await asyncio.gather(*tasks) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| asyncio.run(start_servers()) | ||
| except KeyboardInterrupt: | ||
| logging.info("Shutting down servers...") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import logging | ||
| import os | ||
| from contextlib import asynccontextmanager | ||
| from typing import Dict, List | ||
|
|
||
| import uvicorn | ||
| from fastapi import FastAPI, HTTPException | ||
| from langchain.agents import create_agent | ||
| from langchain.tools import tool | ||
| from langchain_mcp_adapters.tools import load_mcp_tools | ||
| from pydantic import BaseModel | ||
| from src.agents.llm import ChatLLM | ||
| from src.agents.mcp_client import MCPClient | ||
| from src.agents.tools import ( | ||
| add_to_cart, | ||
| checkout, | ||
| empty_cart, | ||
| get_ads, | ||
| get_cart, | ||
| get_product, | ||
| get_recommendations, | ||
| get_shipping_quote, | ||
| get_supported_currencies, | ||
| list_products, | ||
| ) | ||
| from traceloop.sdk.decorators import workflow | ||
|
|
||
|
|
||
| class ChatRequest(BaseModel): | ||
| message: str | ||
| history: List[Dict] = [] | ||
|
|
||
|
|
||
| class Agent: | ||
| def __init__(self): | ||
| self.app = FastAPI(lifespan=self.lifespan) | ||
| self.app.post("/prompt")(self.handle_prompt) | ||
| self.agentRecursionLimit = int(os.getenv("GRAPH_RECURSION_LIMIT", "25")) | ||
| self.mcp_server_url = f"http://{os.getenv('MCP_ENDPOINT', '0.0.0.0')}:{os.getenv('MCP_PORT', '8011')}/mcp" | ||
|
|
||
| self.mcp_server = None | ||
|
|
||
| @asynccontextmanager | ||
| async def lifespan(self, app: FastAPI): | ||
| mcp_enabled = os.getenv("MCP_ENABLED", "False") == "True" | ||
| if mcp_enabled: | ||
| logging.info("MCP tools enabled") | ||
| self.mcp_server = MCPClient() | ||
| await self.mcp_server.connect_to_mcp_server(self.mcp_server_url) | ||
| yield | ||
| if self.mcp_server: | ||
| await self.mcp_server.cleanup() | ||
|
|
||
| async def handle_prompt(self, request: ChatRequest): | ||
| return await self.run_agent(request.message) | ||
|
|
||
| async def get_tool_list(self): | ||
| mcp_enabled = os.getenv("MCP_ENABLED", "False") == "True" | ||
| if mcp_enabled and self.mcp_server is not None: | ||
| return await load_mcp_tools(self.mcp_server.session) | ||
| else: | ||
| tool_list = [ | ||
| add_to_cart, | ||
| checkout, | ||
| convert_currency, | ||
| empty_cart, | ||
| get_ads, | ||
| get_cart, | ||
| get_product, | ||
| get_recommendations, | ||
| get_shipping_quote, | ||
| get_supported_currencies, | ||
| list_products, | ||
| ] | ||
| return [tool(t) for t in tool_list] | ||
|
|
||
| @workflow(name="astronomy_shop_agent_workflow") | ||
| async def run_agent(self, input_prompt): | ||
| model = ChatLLM() | ||
| tools = await self.get_tool_list() | ||
| agent = create_agent( | ||
| model, | ||
| tools=tools, | ||
| system_prompt="You are a helpful assistant. Be concise and accurate.", | ||
| ) | ||
| try: | ||
| result = await agent.ainvoke( | ||
| {"messages": [{"role": "user", "content": input_prompt}]} | ||
| ) | ||
| return {"response": result} | ||
| except Exception as e: | ||
| raise HTTPException(status_code=500, detail=str(e)) | ||
|
|
||
| async def launch(self): | ||
| agent_port = int(os.getenv("AGENT_PORT", "8010")) | ||
| agent_config = uvicorn.Config( | ||
| app=self.app, | ||
| host="0.0.0.0", | ||
| port=agent_port, | ||
| log_level="info", | ||
| ) | ||
| agent_server = uvicorn.Server(agent_config) | ||
| await agent_server.serve() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The agent, mcp, and chatbot services are missing OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES environment variables that all other services define. For an observability demo, telemetry data from these services won't be properly labeled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added for 3 new services