# You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python) # OpenAI Chat completion import os from openai import AsyncOpenAI # importing openai for API usage import chainlit as cl # importing chainlit for our app from chainlit.prompt import Prompt, PromptMessage # importing prompt tools from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools from dotenv import load_dotenv from langchain.document_loaders import CSVLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_openai import OpenAIEmbeddings from langchain.embeddings import CacheBackedEmbeddings from langchain.storage import LocalFileStore from langchain_community.vectorstores import FAISS from datasets import load_dataset load_dotenv() # ChatOpenAI Templates system_template = """You are a helpful assistant who always speaks in a pleasant tone! """ user_template = """{input} Think through your response step by step. """ def setup(): dataset = load_dataset("ShubhamChoksi/IMDB_Movies") print(dataset['train'][0]) print("data from huggingface dataset\n") dataset_dict = dataset dataset_dict["train"] # TODO - what method do we have to use to store imdb.csv from ShubhamChoksi/IMDB_Movies? dataset_dict["train"].to_csv("imdb.csv") loader = CSVLoader(file_path='imdb.csv') data = loader.load() len(data) print(data[0]) print("loaded data from csv\n") text_splitter = RecursiveCharacterTextSplitter( chunk_size = 1000, chunk_overlap = 100, ) chunked_documents = text_splitter.split_documents(data) len(chunked_documents) # ensure we have actually split the data into chunks print(chunked_documents[0]) openai_api_key = os.getenv("OPENAI_API_KEY") embedding_model = OpenAIEmbeddings(openai_api_key=openai_api_key) store = LocalFileStore("./cache/") embedder = CacheBackedEmbeddings.from_bytes_store( embedding_model, store, namespace=embedding_model.model ) vector_store = FAISS.from_documents(chunked_documents, embedder) vector_store.save_local("./vector_store") vector_store = FAISS.load_local("./vector_store", embedder, allow_dangerous_deserialization=True) retriever = vector_store.as_retriever() query = "What are some good westerns movies?" embedded_query = embedding_model.embed_query(query) similar_documents = vector_store.similarity_search_by_vector(embedded_query) for page in similar_documents: # TODO: Print the similar documents that the similarity search returns? print(page) print("00-----0000") print(page) print("-------------") @cl.on_chat_start # marks a function that will be executed at the start of a user session async def start_chat(): settings = { "model": "gpt-3.5-turbo", "temperature": 0, "max_tokens": 500, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0, } setup() cl.user_session.set("settings", settings) @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user async def main(message: cl.Message): settings = cl.user_session.get("settings") client = AsyncOpenAI() print(message.content) prompt = Prompt( provider=ChatOpenAI.id, messages=[ PromptMessage( role="system", template=system_template, formatted=system_template, ), PromptMessage( role="user", template=user_template, formatted=user_template.format(input=message.content), ), ], inputs={"input": message.content}, settings=settings, ) print([m.to_openai() for m in prompt.messages]) msg = cl.Message(content="") # Call OpenAI async for stream_resp in await client.chat.completions.create( messages=[m.to_openai() for m in prompt.messages], stream=True, **settings ): token = stream_resp.choices[0].delta.content if not token: token = "" await msg.stream_token(token) # Update the prompt object with the completion prompt.completion = msg.content msg.prompt = prompt # Send and close the message stream await msg.send()