Spaces:
Runtime error
Runtime error
| import openai | |
| import os | |
| openai.api_key = os.getenv('api_key') | |
| print (openai.api_key) | |
| import langchain | |
| import gradio as gr | |
| import pinecone | |
| from langchain.embeddings.openai import OpenAIEmbeddings | |
| model_name = 'text-embedding-ada-002' | |
| embed = OpenAIEmbeddings( | |
| model=model_name, | |
| openai_api_key=openai.api_key | |
| ) | |
| index_name = 'gideon' | |
| # find API key in console at app.pinecone.io | |
| PINECONE_API_KEY = os.getenv('pine_key') | |
| # find ENV (cloud region) next to API key in console | |
| PINECONE_ENVIRONMENT = "asia-southeast1-gcp-free" | |
| pinecone.init( | |
| api_key=PINECONE_API_KEY, | |
| environment=PINECONE_ENVIRONMENT | |
| ) | |
| from langchain.vectorstores import Pinecone | |
| text_field = "text" | |
| # switch back to normal index for langchain | |
| index = pinecone.Index(index_name) | |
| vectorstore = Pinecone( | |
| index, embed.embed_query, text_field | |
| ) | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.chains import RetrievalQA | |
| # completion llm | |
| llm = ChatOpenAI( | |
| openai_api_key=openai.api_key, | |
| model_name='gpt-3.5-turbo', | |
| temperature=0.2, | |
| stop = None, | |
| max_tokens = 512 | |
| ) | |
| qa = RetrievalQA.from_chain_type( | |
| llm=llm, | |
| chain_type="stuff", | |
| retriever=vectorstore.as_retriever(search_kwargs={"k": 1}) | |
| ) | |
| def langchain_qa(question): | |
| answer = qa.run(question) | |
| return answer | |
| examples = [ | |
| ["What does memory mean in Langchain?"], | |
| ["What real-world problems can be solved by creating agents in Langchain?"], | |
| ["Explain with a real-world example the difference between an agent and a tool in Langchain"] | |
| ] | |
| iface = gr.Interface(fn=langchain_qa, | |
| inputs=gr.inputs.Textbox(lines=2, placeholder='What do you want to know about Langchain?'), | |
| outputs='text', | |
| title='LangGenie: Your personal LangChain Assistant', | |
| description='This application provides answers to your queries about Langchain. Feel free to ask anything related to Langchain features, modules, or use-cases.', | |
| examples=examples) | |
| iface.launch() |