File size: 2,612 Bytes
72a5f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167f934
72a5f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167f934
72a5f6e
7d15da5
72a5f6e
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pdb

import pandas as pd
import pinecone
from dotenv import load_dotenv
import injection
from langchain.vectorstores import Pinecone as pineconeVectorStore
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings import CacheBackedEmbeddings
from langchain.storage import LocalFileStore
import chainlit as cl


load_dotenv()
index_name = 'moviereviews'
import pinecone

# create a new index
pinecone.init(
    api_key='f8f8aa14-3087-44f8-a933-1aa0d335221a',
    environment='gcp-starter'
)
index = pinecone.Index('moviereviews')


# create embedder
store = LocalFileStore("./cache/")

core_embeddings_model = OpenAIEmbeddings()

embedder = CacheBackedEmbeddings.from_bytes_store(
    core_embeddings_model, store, namespace=core_embeddings_model.model
)

# inject data if have not
# injection.inject(index, embedder, "./data/barbie.csv")

# create vectorstore from index and embedder
text_field = "text"
vectorstore = pineconeVectorStore(
    index, embedder.embed_query, text_field
)
model_name = "gpt-4"

from langchain.llms.openai import OpenAIChat

llm = OpenAIChat(model=model_name, temperature=0)
retriever = vectorstore.as_retriever()
from langchain.chains import RetrievalQA
from langchain.callbacks import StdOutCallbackHandler

handler = StdOutCallbackHandler()

qa_with_sources_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    callbacks=[handler],
    return_source_documents=True
)



@cl.on_chat_start # marks a function that will be executed at the start of a user session
def start_chat():
    cl.user_session.set(
        "message_history",
        [{"role": "system", "content": "You are a helpful assistant."}],
    )
    settings = {
        "temperature": 0.7, # higher value increases output diveresity/randomness
        "max_tokens": 500, # maximum length of output response
        "top_p": 1, # choose only the top x% of possible words to return
        "frequency_penalty": 0, # higher value will result in the model being more conservative in its use of repeated tokens.
        "presence_penalty": 0, # higher value will result in the model being more likely to generate tokens that have not yet been included in the generated text
    }
    cl.user_session.set("settings", settings)


@cl.on_message  # this function will be called every time a user inputs a message in the UI
async def main(message: str):
    message_history = cl.user_session.get("message_history")

    result = qa_with_sources_chain({"query" : message})
    msg = cl.Message(content="")
    msg.content = result["result"]

    await msg.send()