-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (74 loc) · 3.12 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Main entrypoint for the app."""
import logging
import pickle
from pathlib import Path
from typing import Optional
from fastapi import FastAPI, Request
from langchain.chains.question_answering import load_qa_chain
from langchain.chat_models import ChatOpenAI
from pydantic import BaseModel
from fastapi.responses import JSONResponse
import json
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ.get("OPENAI_API_KEY")
list_prompts = ["practice_affirmation",
"guided_journaling",
"practice_breathing",
"filler or counselling",
"practice_awareness",
"send_love_to_yourself"]
prompt = """
Imagine a patient is experiencing discomfort due to feelings of self-doubt and low self-esteem. A counselor may provide some advice and suggest the most relevant tools from the following list: ["practice_affirmation", "guided_journaling", "practice_breathing", "filler or counseling", "practice_awareness", "send_love_to_yourself"].
Please generate a response in the following JSON format:
{
"counseling": "{}",
"tools": [
{
"name": "name of the suggested tool",
"prompt": "a detailed prompt or instruction for the user to follow",
"meta": "additional information required for specific tools (if applicable)"
}
],
"message": "a warm, closing message for the patient"
}
For the following tools, please include the specified meta tags:
practice_breathing: Include a meta tag object containing one relevant suggested breathing exercise from the following options: [5-5-5-5, 4-7-8, 4-5-6, 4-4].
practice_affirmation: Include a meta tag object containing a list of all the affirmative statements used in the prompt.
practice_awareness: Include a meta tag object containing the corresponding awareness number used in the prompt.
Please generate a detailed counseling plan for the patient, considering their feelings of self-doubt and low self-esteem, and provide specific prompts for the selected tools.
"""
class InputData(BaseModel):
data: str
class GenResponse(BaseModel):
message: str
app = FastAPI()
@app.on_event("startup")
async def startup_event():
logging.info("loading docsearch")
if not Path("docsearch.pkl").exists():
raise ValueError("docsearch.pkl does not exist. Make sure you run ingest.py")
with open("docsearch.pkl", "rb") as f:
global vectorstore
vectorstore = pickle.load(f)
@app.get("/")
async def get(request: Request):
return {'message': 'Hello World! This is Being AI ~~~'}
@app.post('/gen')
async def gen_endpoint(input_data: InputData):
while True:
try:
chain = load_qa_chain(ChatOpenAI(openai_api_key=api_key), chain_type="stuff")
query = f"{input_data.data}, {prompt}"
docs = vectorstore.similarity_search(query)
resp = chain.run(input_documents=docs, question=query)
jsonify = json.loads(resp)
return JSONResponse(jsonify)
except Exception as e:
logging.error(e)
resp = GenResponse(message="Oops. Something went wrong. Try again.")
return JSONResponse(content=resp)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)