File size: 1,464 Bytes
3c4778e 65932a8 3c4778e 65932a8 3c4778e 65932a8 3c4778e | 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 | import gradio as gr
import os
import getpass
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.vectorstores import DeepLake
from dotenv import load_dotenv
load_dotenv()
os.environ.get("ACTIVELOOP_TOKEN")
username = "rihp" # replace with your username from app.activeloop.ai
projectname = "polywrap5" # replace with your project name from app.activeloop.ai
embeddings = OpenAIEmbeddings(disallowed_special=())
db = DeepLake(dataset_path=f"hub://{username}/{projectname}", read_only=True, embedding_function=embeddings)
retriever = db.as_retriever()
retriever.search_kwargs['distance_metric'] = 'cos'
retriever.search_kwargs['fetch_k'] = 100
retriever.search_kwargs['maximal_marginal_relevance'] = True
retriever.search_kwargs['k'] = 10
model = ChatOpenAI(model_name='gpt-3.5-turbo') # switch to 'gpt-4'
qa = ConversationalRetrievalChain.from_llm(model, retriever=retriever)
def model(prompt):
questions = [
prompt
]
chat_history = []
for question in questions:
result = qa({"question": question, "chat_history": chat_history})
chat_history.append((question, result['answer']))
print(f"-> **Question**: {question} \n")
print(f"**Answer**: {result['answer']} \n")
return result['answer']
iface = gr.Interface(fn=model, inputs="text", outputs="text")
iface.launch() |