import gradio as gr import os import pinecone from dotenv import load_dotenv from langchain.chains import RetrievalQA from langchain.vectorstores import Pinecone from langchain.chat_models import ChatOpenAI from langchain.embeddings import HuggingFaceInferenceAPIEmbeddings load_dotenv() # APIS OPENAI_API_KEY=os.getenv("OPENAI_API_KEY") PINECONE_API_KEY=os.getenv("PINECONE_API_KEY") PINECONE_ENV=os.getenv("PINECONE_ENV") PINECONE_INDEX=os.getenv("PINECONE_INDEX") TEXT_EMBEDDING_MODEL=os.getenv("TEXT_EMBEDDING_MODEL") HF_MODEL=os.getenv("HF_MODEL") HF_API=os.getenv("HF_API") def model(query): pinecone.init( api_key=PINECONE_API_KEY, # find at app.pinecone.io environment=PINECONE_ENV, # next to api key in console ) embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=HF_API ,model_name=HF_MODEL) vectorstore = Pinecone.from_existing_index(PINECONE_INDEX, embeddings) docs = vectorstore.similarity_search(query,k=5) llm = ChatOpenAI(model_name="gpt-3.5-turbo",temperature=0.76, max_tokens=100, model_kwargs={"seed":235, "top_p":0.01}) chain = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=vectorstore.as_retriever()) answer=chain.run({"query": query + "you are a therapist who help people with personal development and self improvement"+ "You can only make conversations related to the provided context. If a response cannot be formed strictly using the context, politely say you don’t have knowledge about that topic."+"[strictly within 75 words]"}) return answer def greet(query): return model(query) iface = gr.Interface(fn=greet, inputs="text", outputs="text") iface.launch()