from fastapi import FastAPI
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import ElasticVectorSearch

from config import openai_api_key

embedding = OpenAIEmbeddings(openai_api_key=openai_api_key)

db = ElasticVectorSearch(
    elasticsearch_url="http://localhost:9200",
    index_name="elastic-index",
    embedding=embedding,
)
qa = RetrievalQA.from_chain_type(
    llm=ChatOpenAI(temperature=0),
    chain_type="stuff",
    retriever=db.as_retriever(),
)

app = FastAPI()


@app.get("/")
def index():
    return {
        "message": "Make a post request to /ask to ask questions about Meditations by Marcus Aurelius"
    }


@app.post("/ask")
def ask(query: str):
    response = qa.run(query)
    return {
        "response": response,
    }

langchain.vectorstores.ElasticVectorSearch langchain.chat_models.ChatOpenAI langchain.embeddings.openai.OpenAIEmbeddings
[((274, 321), 'langchain.embeddings.openai.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'openai_api_key': 'openai_api_key'}), '(openai_api_key=openai_api_key)\n', (290, 321), False, 'from langchain.embeddings.openai import OpenAIEmbeddings\n'), ((328, 444), 'langchain.vectorstores.ElasticVectorSearch', 'ElasticVectorSearch', ([], {'elasticsearch_url': '"""http://localhost:9200"""', 'index_name': '"""elastic-index"""', 'embedding': 'embedding'}), "(elasticsearch_url='http://localhost:9200', index_name=\n    'elastic-index', embedding=embedding)\n", (347, 444), False, 'from langchain.vectorstores import ElasticVectorSearch\n'), ((590, 599), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (597, 599), False, 'from fastapi import FastAPI\n'), ((497, 522), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'temperature': '(0)'}), '(temperature=0)\n', (507, 522), False, 'from langchain.chat_models import ChatOpenAI\n')]