-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_with_code.py
66 lines (56 loc) · 2.51 KB
/
chat_with_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from phi.agent import Agent
from phi.tools.github import GithubTools
from langchain_openai import OpenAIChat
from phi.storage.agent.postgres import PgAgentStorage
from dotenv import load_dotenv
import os
import uuid
load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')
github_access_token = os.getenv("GITHUB_ACCESS_TOKEN")
agent_storage = PgAgentStorage(
table_name="agent_sessions",
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"
)
app = FastAPI()
# Pydantic model for query requests
class QueryRequest(BaseModel):
query: str = Field(..., description="The query or action to perform on the repository.")
repository_url: str = Field(None, description="Optional URL of the repository.")
# Define the agent with tools to interact with GitHub and summarize code
agent = Agent(
name="GitHub Code Summarizer",
description="You are a pro code reviewer, you can read, analyze and understand codes",
model=OpenAIChat(id='gpt-4o-mini'),
tools=[GithubTools(access_token=github_access_token)],
instructions=[
"First get the repo if available and search the whole repository using the tool",
"If no repository url is passed, search for the similar codes in the github using the tool",
"Read the whole repository if any chosen from the previous messages or any repo pointed out",
"Answer the questions based on the code you have read and you have access to",
],
show_tool_calls=True,
session_id=str(uuid.uuid4),
user_id=str(uuid.uuid4), #Sample ids
add_history_to_messages=True,
num_history_responses=10,
storage=agent_storage,
)
@app.post("/summarize_repo")
async def summarize_repository(query_request: QueryRequest):
repo_name = query_request.query
repository_url = query_request.repository_url
if not repo_name:
return JSONResponse({"error": "No repository name provided"}, status_code=400)
try:
# Use the agent to analyze the repository
if repository_url:
response = agent.run(f"Summarize the repository {repo_name} located at {repository_url} and explain its code.")
else:
response = agent.run(f"Summarize the repository {repo_name} and explain its code.")
return JSONResponse({"response": response.to_dict().get('content')})
except Exception as e:
return JSONResponse({"error": str(e)}, status_code=500)