-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
38 lines (31 loc) · 1.56 KB
/
Copy pathserver.py
File metadata and controls
38 lines (31 loc) · 1.56 KB
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
import os
import sys
from dotenv import load_dotenv
load_dotenv()
from psychicapi import Psychic, ConnectorId
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.llms import OpenAI
from langchain.chains import RetrievalQAWithSourcesChain
from fastapi import FastAPI, Request
# Create a document loader for Notion. We can also load from other connectors e.g. ConnectorId.gdrive
psychic = Psychic(secret_key=os.getenv("PSYCHIC_SECRET_KEY"))
raw_docs = psychic.get_documents(connector_id=ConnectorId.notion, account_id="account_id") #replace account_id with the account ID you set while creating a new connection at https://dashboard.psychic.dev/playground
documents = [
Document(page_content=doc["content"], metadata={"title": doc["title"], "source": doc["uri"]},)
for doc in raw_docs
]
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=250) ## Setting an overlap for the CharacterTextSplitter helps improve results
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
vdb = Chroma.from_documents(texts, embeddings)
app = FastAPI()
@app.get("/get_answer")
async def get_answer(request: Request):
chain = RetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type="stuff", retriever=vdb.as_retriever())
body = await request.json()
query = body["query"]
answer = chain({"question": query}, return_only_outputs=True)
return {"answer": answer}