import gradio as gr import openai import json import os OPENAI_SECRET_TOKEN = os.getenv("OPENAI_SECRET_TOKEN") PINECONE_SECRET_TOKEN = os.getenv("PINECONE_SECRET_TOKEN") from pinecone import Pinecone pc = Pinecone(api_key=PINECONE_SECRET_TOKEN) index_name = 'langchaindocs' # connect to index index = pc.Index(index_name) # get api key from platform.openai.com openai.api_key = OPENAI_SECRET_TOKEN embed_model = "text-embedding-ada-002" from openai import OpenAI client = OpenAI(api_key=openai.api_key) def generate_context(text): body=json.dumps({"inputText": text}) res = client.embeddings.create( input=[text], model=embed_model ).data[0].embedding result = index.query(vector=res, top_k=20, include_metadata=True) contexts = [] contexts = contexts + [x['metadata']['text'] for x in result['matches']] return contexts def invoke_openai(prompt,history): sys_prompt = "You are a helpful assistant that always answers questions." new_messages=[ {"role": "system", "content": sys_prompt}, {"role": "system", "content": "Use only the context to answer the question"}, ] for conv in history: user = conv[0] new_messages.append({"role": "user", "content":user }) assistant = conv[1] new_messages.append({"role": "assistant", "content":assistant}) new_messages.append({"role": "user", "content": prompt}) # query text-davinci-003 res = client.chat.completions.create( model='gpt-3.5-turbo', messages=new_messages, temperature=0 ) return res.choices[0].message.content def build_prompt(message,history): context=generate_context(message) prompt=f'Context - {context}\nBased on the above context, answer this question - {message}' print(prompt) return invoke_openai(prompt,history) iface = gr.ChatInterface(build_prompt, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="GenAIx", examples = ["How coversational chat works","Can you write a code snippet for conversation bot?"], theme="soft", cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear",) iface.launch(share=True)