|
| 1 | +import 'dotenv/config'; |
| 2 | +import OpenAI from 'openai'; |
| 3 | + |
| 4 | +import { LiteralClient } from '../src'; |
| 5 | + |
| 6 | +const literalClient = new LiteralClient(); |
| 7 | + |
| 8 | +const openai = new OpenAI(); |
| 9 | + |
| 10 | +// Instrument the OpenAI client |
| 11 | +literalClient.instrumentation.openai(); |
| 12 | + |
| 13 | +async function main() { |
| 14 | + // This will be automagically logged by Literal AI |
| 15 | + await openai.chat.completions.create({ |
| 16 | + model: 'gpt-4', |
| 17 | + messages: [{ role: 'user', content: 'Say this is a test' }] |
| 18 | + }); |
| 19 | + |
| 20 | + await openai.chat.completions.create({ |
| 21 | + model: 'gpt-4', |
| 22 | + messages: [ |
| 23 | + { |
| 24 | + role: 'user', |
| 25 | + content: |
| 26 | + 'Write a README document in markdown explaining the basic usage of the `with-csv` npm library.' |
| 27 | + } |
| 28 | + ] |
| 29 | + }); |
| 30 | + |
| 31 | + await literalClient |
| 32 | + .run({ |
| 33 | + name: 'Test run', |
| 34 | + input: { |
| 35 | + content: |
| 36 | + '{"question": ["how to code chat with pdf and use pdfplumber and ollama local",[]],"images_content": {}}' |
| 37 | + }, |
| 38 | + output: { |
| 39 | + content: `To create a chat application that interacts with PDF files using \`pdfplumber\` and the Ollama local model in Chainlit, you can follow the example below. This example demonstrates how to upload a PDF, extract text using \`pdfplumber\`, and allow users to ask questions about its content using the Ollama model. |
| 40 | + |
| 41 | + ### Step 1: Install Required Packages |
| 42 | + Make sure you have the necessary packages installed: |
| 43 | + \`\`\`bash |
| 44 | + pip install chainlit pdfplumber langchain |
| 45 | + \`\`\` |
| 46 | + |
| 47 | + ### Step 2: Create the Application Script |
| 48 | + Create a Python file named \`app.py\` and add the following code: |
| 49 | + |
| 50 | + \`\`\`python |
| 51 | + import os |
| 52 | + import pdfplumber |
| 53 | + import chainlit as cl |
| 54 | + from langchain.llms.ollama import Ollama |
| 55 | + from langchain.prompts import ChatPromptTemplate |
| 56 | + from langchain.schema import StrOutputParser |
| 57 | + from langchain.schema.runnable import Runnable |
| 58 | + from langchain.schema.runnable.config import RunnableConfig |
| 59 | + from langchain.text_splitter import RecursiveCharacterTextSplitter |
| 60 | + from langchain.memory import ChatMessageHistory, ConversationBufferMemory |
| 61 | + from langchain.schema import Document |
| 62 | + |
| 63 | + # Initialize the Ollama model |
| 64 | + model = Ollama(model="llama2") |
| 65 | + |
| 66 | + @cl.on_chat_start |
| 67 | + async def on_chat_start(): |
| 68 | + files = await cl.AskFileMessage( |
| 69 | + content="Please upload a PDF file to start asking questions.", |
| 70 | + accept=["application/pdf"], |
| 71 | + max_size_mb=20, |
| 72 | + timeout=180, |
| 73 | + ).send() |
| 74 | + |
| 75 | + file = files[0] |
| 76 | + docs = process_pdf(file) |
| 77 | + |
| 78 | + message_history = ChatMessageHistory() |
| 79 | + memory = ConversationBufferMemory(memory_key="chat_history", output_key="answer", chat_memory=message_history, return_messages=True) |
| 80 | + |
| 81 | + prompt = ChatPromptTemplate.from_messages( |
| 82 | + [ |
| 83 | + ("system", "You're a knowledgeable assistant who provides accurate answers based on the PDF content."), |
| 84 | + ("human", "{question}"), |
| 85 | + ] |
| 86 | + ) |
| 87 | + |
| 88 | + runnable = prompt | model | StrOutputParser() |
| 89 | + cl.user_session.set("runnable", runnable) |
| 90 | + |
| 91 | + await cl.Message(content="You can now ask questions about the PDF!").send() |
| 92 | + |
| 93 | + def process_pdf(file): |
| 94 | + text = "" |
| 95 | + with pdfplumber.open(file.path) as pdf: |
| 96 | + for page in pdf.pages: |
| 97 | + text += page.extract_text() + "\ |
| 98 | + " |
| 99 | + |
| 100 | + # Split the text into chunks |
| 101 | + text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100) |
| 102 | + docs = text_splitter.split_text(text) |
| 103 | + documents = [Document(page_content=chunk) for chunk in docs] |
| 104 | + return documents |
| 105 | + |
| 106 | + @cl.on_message |
| 107 | + async def on_message(message: cl.Message): |
| 108 | + runnable = cl.user_session.get("runnable") # type: Runnable |
| 109 | + msg = cl.Message(content="") |
| 110 | + |
| 111 | + for chunk in await cl.make_async(runnable.stream)( |
| 112 | + {"question": message.content}, |
| 113 | + config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]), |
| 114 | + ): |
| 115 | + await msg.stream_token(chunk) |
| 116 | + |
| 117 | + await msg.send() |
| 118 | + \`\`\` |
| 119 | + |
| 120 | + ### Step 3: Run the Application |
| 121 | + To start the Chainlit application, run the following command in your terminal: |
| 122 | + \`\`\`bash |
| 123 | + chainlit run app.py |
| 124 | + \`\`\` |
| 125 | + |
| 126 | + ### Step 4: Interact with the Application |
| 127 | + Open your browser and navigate to \`http://localhost:8000\`. You can upload a PDF file and start asking questions about its content. |
| 128 | + |
| 129 | + ### Explanation |
| 130 | + - **PDF Processing**: The \`process_pdf\` function uses \`pdfplumber\` to extract text from the uploaded PDF file. |
| 131 | + - **Text Splitting**: The extracted text is split into manageable chunks using \`RecursiveCharacterTextSplitter\`. |
| 132 | + - **Ollama Model**: The Ollama model is used to generate responses based on the extracted text. |
| 133 | + - **Conversational Interface**: Users can ask questions, and the model will respond based on the content of the PDF. |
| 134 | + |
| 135 | + This setup allows you to create a conversational interface that can answer questions based on the content of a PDF file using \`pdfplumber\` and the Ollama local model.` |
| 136 | + } |
| 137 | + }) |
| 138 | + .send(); |
| 139 | +} |
| 140 | + |
| 141 | +main(); |
0 commit comments