Spaces:
Sleeping
Sleeping
Commit Β·
01038c9
1
Parent(s): fb07b17
Refactor application structure by removing backend directory and integrating main.py directly. Update Dockerfile to reflect new entry point and streamline dependencies. Implement FastAPI with CORS support and asynchronous agent handling for improved functionality.
Browse files- backend/.env β .env +0 -0
- Dockerfile +1 -1
- {backend/agent β agent}/__init__.py +0 -0
- {backend/agent β agent}/__pycache__/__init__.cpython-313.pyc +0 -0
- {backend/agent β agent}/__pycache__/linkedin_assistant.cpython-313.pyc +0 -0
- {backend/agent β agent}/linkedin_assistant.py +0 -0
- backend/.dockerignore +0 -1
- backend/.env.example +0 -5
- backend/.python-version +0 -1
- backend/Dockerfile +0 -25
- backend/README.md +0 -0
- backend/compose.yml +0 -10
- backend/main.py +0 -85
- backend/mcp_server_tools/__init__.py +0 -0
- backend/pyproject.toml +0 -16
- backend/uv.lock +0 -0
- main.py +83 -5
- {backend/mcp_client_tools β mcp_client_tools}/__init__.py +0 -0
- {backend/mcp_client_tools β mcp_client_tools}/__pycache__/__init__.cpython-313.pyc +0 -0
- {backend/mcp_client_tools β mcp_client_tools}/__pycache__/mcp_client.cpython-313.pyc +0 -0
- {backend/mcp_client_tools β mcp_client_tools}/mcp_client.py +0 -0
- {backend β mcp_server_tools}/__init__.py +0 -0
- {backend/mcp_server_tools β mcp_server_tools}/__pycache__/raw_function.cpython-313.pyc +0 -0
- {backend/mcp_server_tools β mcp_server_tools}/mcp_server_linkedin.py +0 -0
- {backend/mcp_server_tools β mcp_server_tools}/mcp_server_weather.py +0 -0
- {backend/mcp_server_tools β mcp_server_tools}/raw_function.py +0 -0
- {backend/tools β tools}/__init__.py +0 -0
- {backend/tools β tools}/__pycache__/__init__.cpython-313.pyc +0 -0
- {backend/tools β tools}/__pycache__/linkedin_post_links.cpython-313.pyc +0 -0
- {backend/tools β tools}/__pycache__/linkedin_posts.cpython-313.pyc +0 -0
- {backend/tools β tools}/__pycache__/linkedin_userinfo.cpython-313.pyc +0 -0
- {backend/tools β tools}/linkedin_group_posts.py +0 -0
- {backend/tools β tools}/linkedin_post_links.py +0 -0
- {backend/tools β tools}/linkedin_posts.py +0 -0
- {backend/tools β tools}/linkedin_userinfo.py +0 -0
backend/.env β .env
RENAMED
|
File without changes
|
Dockerfile
CHANGED
|
@@ -13,4 +13,4 @@ COPY --chown=user ./requirements.txt requirements.txt
|
|
| 13 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
|
| 15 |
COPY --chown=user . /app
|
| 16 |
-
CMD ["uvicorn", "
|
|
|
|
| 13 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
|
| 15 |
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
{backend/agent β agent}/__init__.py
RENAMED
|
File without changes
|
{backend/agent β agent}/__pycache__/__init__.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/agent β agent}/__pycache__/linkedin_assistant.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/agent β agent}/linkedin_assistant.py
RENAMED
|
File without changes
|
backend/.dockerignore
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
.venv
|
|
|
|
|
|
backend/.env.example
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
GOOGLE_API_KEY=""
|
| 2 |
-
|
| 3 |
-
LINKEDIN_ACCESS_TOKEN=""
|
| 4 |
-
|
| 5 |
-
LANGSMITH_API_KEY=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/.python-version
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
3.13
|
|
|
|
|
|
backend/Dockerfile
DELETED
|
@@ -1,25 +0,0 @@
|
|
| 1 |
-
# Base image for all services
|
| 2 |
-
FROM python:3.13-slim
|
| 3 |
-
|
| 4 |
-
# Install uv for dependency management
|
| 5 |
-
RUN pip install uv
|
| 6 |
-
|
| 7 |
-
WORKDIR /app/
|
| 8 |
-
|
| 9 |
-
COPY pyproject.toml uv.lock ./
|
| 10 |
-
|
| 11 |
-
# Copy the main script and the agent directory
|
| 12 |
-
COPY main.py ./
|
| 13 |
-
COPY agent ./agent/
|
| 14 |
-
COPY tools ./tools/
|
| 15 |
-
COPY mcp_server_tools ./mcp_server_tools/
|
| 16 |
-
COPY mcp_client_tools ./mcp_client_tools/
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
RUN uv sync --frozen
|
| 20 |
-
|
| 21 |
-
# Expose all necessary ports
|
| 22 |
-
EXPOSE 8002
|
| 23 |
-
|
| 24 |
-
# Use the start script as the entry point
|
| 25 |
-
CMD ["uv","run","uvicorn","main:app","--port","8002","--host","0.0.0.0","--reload"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/README.md
DELETED
|
File without changes
|
backend/compose.yml
DELETED
|
@@ -1,10 +0,0 @@
|
|
| 1 |
-
services:
|
| 2 |
-
agent-backend:
|
| 3 |
-
container_name: Agent-Container
|
| 4 |
-
build:
|
| 5 |
-
context: ./
|
| 6 |
-
dockerfile: Dockerfile
|
| 7 |
-
ports:
|
| 8 |
-
- 8002:8002
|
| 9 |
-
env_file:
|
| 10 |
-
- .env
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/main.py
DELETED
|
@@ -1,85 +0,0 @@
|
|
| 1 |
-
from contextlib import asynccontextmanager
|
| 2 |
-
from langsmith.wrappers import OpenAIAgentsTracingProcessor
|
| 3 |
-
from agent.linkedin_assistant import create_assistant_agent
|
| 4 |
-
from openai.types.responses import ResponseTextDeltaEvent
|
| 5 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
from agents import Runner, set_trace_processors
|
| 7 |
-
from fastapi.responses import StreamingResponse
|
| 8 |
-
from dotenv import find_dotenv, load_dotenv
|
| 9 |
-
from fastapi import FastAPI, HTTPException
|
| 10 |
-
from pydantic import BaseModel
|
| 11 |
-
import json
|
| 12 |
-
import os
|
| 13 |
-
|
| 14 |
-
load_dotenv(find_dotenv())
|
| 15 |
-
|
| 16 |
-
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 17 |
-
os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGSMITH_API_KEY")
|
| 18 |
-
os.environ["LANGCHAIN_PROJECT"] = "linkedin"
|
| 19 |
-
|
| 20 |
-
set_trace_processors([OpenAIAgentsTracingProcessor()])
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
@asynccontextmanager
|
| 24 |
-
async def lifespan(app: FastAPI):
|
| 25 |
-
global assistant_agent
|
| 26 |
-
try:
|
| 27 |
-
from agent.linkedin_assistant import create_assistant_agent
|
| 28 |
-
assistant_agent = await create_assistant_agent()
|
| 29 |
-
print("[β] Assistant agent initialized.")
|
| 30 |
-
yield
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"[β] Failed to initialize assistant agent: {e}")
|
| 33 |
-
raise # Let FastAPI shutdown cleanly on failure
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
app: FastAPI = FastAPI(lifespan=lifespan)
|
| 37 |
-
|
| 38 |
-
# Add CORS middleware
|
| 39 |
-
app.add_middleware(
|
| 40 |
-
CORSMiddleware,
|
| 41 |
-
allow_origins=["*"],
|
| 42 |
-
allow_credentials=True,
|
| 43 |
-
allow_methods=["*"],
|
| 44 |
-
allow_headers=["*"],
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
# @app.on_event("startup")
|
| 48 |
-
# async def startup_event():
|
| 49 |
-
# global assistant_agent
|
| 50 |
-
# assistant_agent = await create_assistant_agent()
|
| 51 |
-
|
| 52 |
-
@app.post("/run")
|
| 53 |
-
async def agent_fastapi(input):
|
| 54 |
-
result = await Runner.run(assistant_agent, input=input)
|
| 55 |
-
print(result.final_output)
|
| 56 |
-
return result.final_output
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
async def stream_response(input):
|
| 60 |
-
result = Runner.run_streamed(assistant_agent, input=input)
|
| 61 |
-
async for events in result.stream_events():
|
| 62 |
-
if events.type == "raw_response_event" and isinstance(
|
| 63 |
-
events.data, ResponseTextDeltaEvent
|
| 64 |
-
):
|
| 65 |
-
chunk = json.dumps(events.data.delta)
|
| 66 |
-
yield chunk
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
class InputData(BaseModel):
|
| 70 |
-
input: str
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
@app.post("/chat/stream")
|
| 74 |
-
async def agent_fastapi_streaming(data: InputData):
|
| 75 |
-
if not data.input:
|
| 76 |
-
raise HTTPException(status_code=400, detail="Message text cannot be empty")
|
| 77 |
-
return StreamingResponse(
|
| 78 |
-
stream_response(data.input), media_type="text/event-stream"
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if __name__ == "__main__":
|
| 83 |
-
import uvicorn
|
| 84 |
-
|
| 85 |
-
uvicorn.run(app="main:app", host="0.0.0.0", port=8002, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/mcp_server_tools/__init__.py
DELETED
|
File without changes
|
backend/pyproject.toml
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
[project]
|
| 2 |
-
name = "backend"
|
| 3 |
-
version = "0.1.0"
|
| 4 |
-
description = "Add your description here"
|
| 5 |
-
readme = "README.md"
|
| 6 |
-
requires-python = ">=3.13"
|
| 7 |
-
dependencies = [
|
| 8 |
-
"aiohttp>=3.11.18",
|
| 9 |
-
"fastapi>=0.115.12",
|
| 10 |
-
"langgraph>=0.3.27",
|
| 11 |
-
"langsmith[openai-agents]>=0.3.28",
|
| 12 |
-
"mcp[cli]>=1.9.0",
|
| 13 |
-
"openai-agents==0.0.15",
|
| 14 |
-
"python-dotenv>=1.1.0",
|
| 15 |
-
"uvicorn>=0.34.0",
|
| 16 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/uv.lock
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
main.py
CHANGED
|
@@ -1,7 +1,85 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from contextlib import asynccontextmanager
|
| 2 |
+
from langsmith.wrappers import OpenAIAgentsTracingProcessor
|
| 3 |
+
from agent.linkedin_assistant import create_assistant_agent
|
| 4 |
+
from openai.types.responses import ResponseTextDeltaEvent
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from agents import Runner, set_trace_processors
|
| 7 |
+
from fastapi.responses import StreamingResponse
|
| 8 |
+
from dotenv import find_dotenv, load_dotenv
|
| 9 |
+
from fastapi import FastAPI, HTTPException
|
| 10 |
+
from pydantic import BaseModel
|
| 11 |
+
import json
|
| 12 |
+
import os
|
| 13 |
|
| 14 |
+
load_dotenv(find_dotenv())
|
| 15 |
|
| 16 |
+
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 17 |
+
os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGSMITH_API_KEY")
|
| 18 |
+
os.environ["LANGCHAIN_PROJECT"] = "linkedin"
|
| 19 |
+
|
| 20 |
+
set_trace_processors([OpenAIAgentsTracingProcessor()])
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@asynccontextmanager
|
| 24 |
+
async def lifespan(app: FastAPI):
|
| 25 |
+
global assistant_agent
|
| 26 |
+
try:
|
| 27 |
+
from agent.linkedin_assistant import create_assistant_agent
|
| 28 |
+
assistant_agent = await create_assistant_agent()
|
| 29 |
+
print("[β] Assistant agent initialized.")
|
| 30 |
+
yield
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"[β] Failed to initialize assistant agent: {e}")
|
| 33 |
+
raise # Let FastAPI shutdown cleanly on failure
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
app: FastAPI = FastAPI(lifespan=lifespan)
|
| 37 |
+
|
| 38 |
+
# Add CORS middleware
|
| 39 |
+
app.add_middleware(
|
| 40 |
+
CORSMiddleware,
|
| 41 |
+
allow_origins=["*"],
|
| 42 |
+
allow_credentials=True,
|
| 43 |
+
allow_methods=["*"],
|
| 44 |
+
allow_headers=["*"],
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# @app.on_event("startup")
|
| 48 |
+
# async def startup_event():
|
| 49 |
+
# global assistant_agent
|
| 50 |
+
# assistant_agent = await create_assistant_agent()
|
| 51 |
+
|
| 52 |
+
@app.post("/run")
|
| 53 |
+
async def agent_fastapi(input):
|
| 54 |
+
result = await Runner.run(assistant_agent, input=input)
|
| 55 |
+
print(result.final_output)
|
| 56 |
+
return result.final_output
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
async def stream_response(input):
|
| 60 |
+
result = Runner.run_streamed(assistant_agent, input=input)
|
| 61 |
+
async for events in result.stream_events():
|
| 62 |
+
if events.type == "raw_response_event" and isinstance(
|
| 63 |
+
events.data, ResponseTextDeltaEvent
|
| 64 |
+
):
|
| 65 |
+
chunk = json.dumps(events.data.delta)
|
| 66 |
+
yield chunk
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class InputData(BaseModel):
|
| 70 |
+
input: str
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@app.post("/chat/stream")
|
| 74 |
+
async def agent_fastapi_streaming(data: InputData):
|
| 75 |
+
if not data.input:
|
| 76 |
+
raise HTTPException(status_code=400, detail="Message text cannot be empty")
|
| 77 |
+
return StreamingResponse(
|
| 78 |
+
stream_response(data.input), media_type="text/event-stream"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# if __name__ == "__main__":
|
| 83 |
+
# import uvicorn
|
| 84 |
+
|
| 85 |
+
# uvicorn.run(app="main:app", host="0.0.0.0", port=8002, reload=True)
|
{backend/mcp_client_tools β mcp_client_tools}/__init__.py
RENAMED
|
File without changes
|
{backend/mcp_client_tools β mcp_client_tools}/__pycache__/__init__.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/mcp_client_tools β mcp_client_tools}/__pycache__/mcp_client.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/mcp_client_tools β mcp_client_tools}/mcp_client.py
RENAMED
|
File without changes
|
{backend β mcp_server_tools}/__init__.py
RENAMED
|
File without changes
|
{backend/mcp_server_tools β mcp_server_tools}/__pycache__/raw_function.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/mcp_server_tools β mcp_server_tools}/mcp_server_linkedin.py
RENAMED
|
File without changes
|
{backend/mcp_server_tools β mcp_server_tools}/mcp_server_weather.py
RENAMED
|
File without changes
|
{backend/mcp_server_tools β mcp_server_tools}/raw_function.py
RENAMED
|
File without changes
|
{backend/tools β tools}/__init__.py
RENAMED
|
File without changes
|
{backend/tools β tools}/__pycache__/__init__.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/tools β tools}/__pycache__/linkedin_post_links.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/tools β tools}/__pycache__/linkedin_posts.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/tools β tools}/__pycache__/linkedin_userinfo.cpython-313.pyc
RENAMED
|
File without changes
|
{backend/tools β tools}/linkedin_group_posts.py
RENAMED
|
File without changes
|
{backend/tools β tools}/linkedin_post_links.py
RENAMED
|
File without changes
|
{backend/tools β tools}/linkedin_posts.py
RENAMED
|
File without changes
|
{backend/tools β tools}/linkedin_userinfo.py
RENAMED
|
File without changes
|