Skip to content
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

Fastapi Logic for Pippy Fixes#1 #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions pippy_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, HTTPException, Request, APIRouter
from pydantic import BaseModel
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
Expand Down Expand Up @@ -34,20 +33,10 @@
Answer: Let's think step by step.
"""

# Initialize FastAPI
app = FastAPI()
router=APIRouter()
chimosky marked this conversation as resolved.
Show resolved Hide resolved

# Enable CORS middleware for frontend interaction
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Replace "*" with specific domains if needed
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

# Define the RAG Agent class
class RAG_Agent:
# Define the Pippy RAG Agent class
chimosky marked this conversation as resolved.
Show resolved Hide resolved
class Pippy_RAG_Agent:
def __init__(self, model="llama3.1"):
self.model = OllamaLLM(model=model)
self.retriever = None
Expand Down Expand Up @@ -164,28 +153,24 @@ def run(self, question):
response = self.model.invoke(question)
return response

# Define a query model for incoming POST requests
class QueryModel(BaseModel):
# Define a class query for incoming POST requests
chimosky marked this conversation as resolved.
Show resolved Hide resolved
class Query(BaseModel):
question: str

# Initialize the RAG Agent and set up the retriever
agent = RAG_Agent()
# Initialize the Pippy RAG Agent and set up the retriever
chimosky marked this conversation as resolved.
Show resolved Hide resolved
agent = Pippy_RAG_Agent()
agent.setup_vectorstore(document_paths)

# Define API routes
@app.get("/")
@router.get("/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does each router require it's own root route?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, each router does not require its root route. I've added it to provide a default response. It acts as an entry point for our API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would they be a need to provide a default response seeing as when pointing the activity to it, you'll use the right url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so shall I replace it with something like /Pippy_AI_Assistant ?

async def root():
return {"message": "Welcome to the AI Coding Assistant!"}
chimosky marked this conversation as resolved.
Show resolved Hide resolved

@app.post("/query/")
async def handle_query(query: QueryModel, request: Request):
@router.post("/query/")
chimosky marked this conversation as resolved.
Show resolved Hide resolved
async def handle_query(query: Query, request: Request):
try:
response = agent.run(query.question)
return {"response": response}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

# Run the FastAPI server
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)