-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathielts_agent.py
60 lines (48 loc) · 1.69 KB
/
ielts_agent.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
import os
from pydantic import BaseModel
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from phi.agent import Agent
from phi.knowledge.csv import CSVKnowledgeBase
from phi.vectordb.pgvector import PgVector
from langchain_openai import OpenAIEmbeddings
from phi.model.openai import OpenAIChat
from dotenv import load_dotenv
load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')
app = FastAPI()
embedding = OpenAIEmbeddings()
ielts_writing = 'sample_data/ielts-writing-essays.csv'
knowledge_base = CSVKnowledgeBase(
path="sample_data",
vector_db=PgVector(
table_name="ielts_writings",
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
),
)
agent = Agent(
name="IELTS Writing tutor",
description="You are a tutor for IELTS writing task",
model=OpenAIChat(id='gpt-4o-mini'),
knowledge_base=knowledge_base,
search_knowledge=True,
instructions=[
"Greet only at the first time",
"Get list of files",
"Analyze the files",
"If the user wanted to write a text for an IELTS writing exam, write a proper text",
"you can find some similar writing answers to inspire by them",
"if the user wanted different styles and scores for writing search knowledge base and show the answers",
]
)
agent.knowledge.load(recreate=False)
class QueryRequest(BaseModel):
query: str
@app.post("/query")
async def query_agent(request: QueryRequest):
user_query = request.query
if not user_query:
return JSONResponse({"error": "No query provided"}, status_code=400)
answer = agent.run(user_query)
answer = answer.to_dict().get('content')
return JSONResponse({"response": answer})