Search

Thursday 30 November 2023

Simple chat app using openai client.chat.completions endpoint after openai version >= 1.2.0

import streamlit as st
import openai
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
# Load environment variables
load_dotenv(find_dotenv())

st.title('OpenAI Chat App')

client = OpenAI()

user_input = st.text_input("You: ", "")
if st.button('Send'):
    response = client.chat.completions.create(
      model="gpt-3.5-turbo",
      messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_input}
        ],
    temperature=0
    )
    st.write('Assistant: ', response.choices[0].message.content)

This is a Python script that uses the Streamlit library to create a web application, and the OpenAI API to generate responses from the GPT-3 model.

Here’s a breakdown of what each part of the code does:

  1. Importing necessary libraries:
import streamlit as st
import openai
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv

These lines import the necessary libraries. streamlit is a library for building web applications, openai is the OpenAI API client, and dotenv is used to load environment variables from a .env file.

  1. Loading environment variables:
load_dotenv(find_dotenv())

This line loads environment variables from a .env file in your project directory. This is typically used to securely store sensitive information like API keys.

  1. Setting up the Streamlit app:
st.title('OpenAI Chat App')

This line sets the title of the web application to ‘OpenAI Chat App’.

  1. Creating an OpenAI client:
client = OpenAI()

This line creates an instance of the OpenAI client, which is used to interact with the OpenAI API.

  1. Getting user input:
user_input = st.text_input("You: ", "")

This line creates a text input field in the web application where the user can enter their message.

  1. Sending the user’s message to the OpenAI API and displaying the response:
if st.button('Send'):
    response = client.chat.completions.create(
      model="gpt-3.5-turbo",
      messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_input}
        ],
    temperature=0
    )
    st.write('Assistant: ', response.choices[0].message.content)

When the ‘Send’ button is clicked, this block of code sends the user’s message to the OpenAI API, along with a system message that sets the behavior of the assistant. The response from the API is then displayed in the web application.

The temperature parameter controls the randomness of the AI’s output. A value of 0 makes the output deterministic, meaning the AI will always choose the most likely next word when generating its response.

Simple Chat App with PDF documents using OpenAIEmbeddings, FAISS index, langchain and streamlit

import streamlit as st
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import pandas as pd

# Load environment variables
load_dotenv(find_dotenv())

# Create an instance of OpenAI
client = OpenAI()

# Create an instance of the OpenAI embeddings
embeddings = OpenAIEmbeddings()

# Create a text splitter
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)

# Load the question answering chain
chain = load_qa_chain(OpenAI(), chain_type="stuff")

# Streamlit app
st.title('Question Answering App')

# Upload the PDF file
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")

if uploaded_file is not None:
    # Read the PDF file
    doc_reader = PdfReader(uploaded_file)

    # Split the text into chunks
    raw_text = ''
    for i, page in enumerate(doc_reader.pages):
        text = page.extract_text()
        if text:
            raw_text += text
           
    texts = text_splitter.split_text(raw_text)

    # Create a FAISS index from the texts
    docsearch = FAISS.from_texts(texts, embeddings)

    # Input the question
    query = st.text_input('Enter your question:')

    if st.button('Answer'):
        # Perform a similarity search
        docs = docsearch.similarity_search(query)

        # Run the chain and get the answer
        answer = chain.run(input_documents=docs, question=query)

        # Display the answer
        st.write(answer)

This Python script is a Streamlit application that uses OpenAI’s language model to answer questions about the content of a PDF file. Here’s a breakdown of what each part of the code does:

Import necessary libraries: The script starts by importing necessary libraries such as streamlit, openai, dotenv, PyPDF2, and several modules from langchain.

Load environment variables: The load_dotenv(find_dotenv()) line loads environment variables from a .env file in your project directory. This is typically used to securely manage sensitive information like API keys.

Create instances of OpenAI and OpenAIEmbeddings: These instances are used to interact with the OpenAI API and to create embeddings (vector representations of text), respectively.

Create a text splitter: This is used to split the text from the PDF into manageable chunks.

Load the question answering chain: This is a sequence of operations that takes a question and a set of documents as input and returns an answer.

Streamlit app setup: The st.title('Question Answering App') line sets the title of the Streamlit app.

Upload the PDF file: The st.file_uploader("Choose a PDF file", type="pdf") line creates a file uploader in the Streamlit app that accepts PDF files.

Read the PDF file and split the text into chunks: If a PDF file is uploaded, the script reads the file, extracts the text from each page, and splits the text into chunks using the text splitter created earlier.

Create a FAISS index from the texts: FAISS is a library for efficient similarity search and clustering of dense vectors. The script creates a FAISS index from the chunks of text, which allows it to quickly find chunks of text that are similar to a given query.

Input the question: The st.text_input('Enter your question:') line creates a text input in the Streamlit app where you can enter your question.

Answer the question: If the ‘Answer’ button is clicked, the script performs a similarity search to find chunks of text that are similar to the question, runs the question answering chain on these chunks, and displays the answer in the Streamlit app.



Simple Chat with PDF documents using OpenAIEmbeddings, FAISS index and langchain

07 chat with pdf document using langchain