This commit is contained in:
+25
-38
@@ -1,44 +1,31 @@
|
||||
"""
|
||||
FastAPI application exposing the RAG agent as a REST endpoint.
|
||||
|
||||
This file is optional but useful for running the agent in a container.
|
||||
"""
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from .agent import RAGAgent
|
||||
|
||||
app = FastAPI(title="RAG Agent API")
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Educational RAG Agent CLI")
|
||||
parser.add_argument("--config", type=str, default="src/config.yaml", help="Path to config file")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Initialize a global agent instance
|
||||
agent = RAGAgent(model="llama2", top_k=3)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
agent = RAGAgent(config_path=args.config)
|
||||
|
||||
print("Welcome to the Educational RAG Agent. Type 'exit' to quit.")
|
||||
while True:
|
||||
try:
|
||||
query = input("\nYour question: ").strip()
|
||||
if query.lower() in ("exit", "quit"):
|
||||
print("Goodbye!")
|
||||
break
|
||||
if not query:
|
||||
print("Please enter a non-empty question.")
|
||||
continue
|
||||
answer = agent.generate_response(query)
|
||||
print(f"\nAnswer:\n{answer}")
|
||||
except KeyboardInterrupt:
|
||||
print("\nInterrupted. Exiting.")
|
||||
break
|
||||
|
||||
class Document(BaseModel):
|
||||
text: str
|
||||
|
||||
|
||||
class Query(BaseModel):
|
||||
query: str
|
||||
|
||||
|
||||
@app.post("/documents")
|
||||
def add_document(doc: Document):
|
||||
"""
|
||||
Add a document to the agent's memory.
|
||||
"""
|
||||
agent.add_document(doc.text)
|
||||
return {"status": "added"}
|
||||
|
||||
|
||||
@app.post("/ask")
|
||||
def ask(query: Query):
|
||||
"""
|
||||
Get an answer to a query using the RAG agent.
|
||||
"""
|
||||
try:
|
||||
answer = agent.get_response(query.query)
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=500, detail=str(exc))
|
||||
return {"answer": answer}
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user