File size: 2,048 Bytes
24f5c47
3e3e691
ada9416
c2796f4
24f5c47
 
 
3e3e691
24f5c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7397eca
24f5c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10953bd
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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()