I am using the gpt-4 model and I want it to be able to read documents and respond to me based on the documents

I am using the gpt-4 model and I want it to be able to read documents and respond to me based on the documents, how can I do that?

Attached example of my code:

from flask import Flask, render_template, request
import openai
import os

app = Flask(name)

key = “XXXXXXXXXXXXXXXXXXXXX”
openai.api_key = key

preguntas = list()
respuestas_gpt = list()
mensajes = list()

@app.route(‘/’)
def index():
return render_template(‘chat.html’)

@app.route(‘/chat’, methods=[‘POST’])
def chat():
pregunta_actual = request.form[‘pregunta’]

if pregunta_actual.lower() in ['exit', 'detener', 'parar', 'stop', 'salir', 'quit']:
    return render_template('chat.html', pregunta='', respuesta='ChatBot: Fue un placer ayudarte')

if pregunta_actual == "":
    return render_template('chat.html', pregunta='', respuesta='')

mensajes.append({'role': 'user', 'content': pregunta_actual})

respuestas = openai.ChatCompletion.create(
    model="gpt-4",
    messages=mensajes,
    temperature=0.7,
    #max_tokens=100
)

respuesta_actual = respuestas.choices[0]['message']['content']
respuestas_gpt.append(respuesta_actual)
mensajes.append({'role': 'assistant', 'content': respuesta_actual})

return render_template('chat.html', pregunta=pregunta_actual, respuesta=f'ChatBot: {respuesta_actual}')

if name == ‘main’:
app.run(debug=True)

1 Like

To enable the GPT-4 model to read documents and respond based on them in your Flask application, you need to modify your code to incorporate document processing. Here’s a suggested approach:

  1. Store and Process Documents: You’ll need to store the documents you want GPT-4 to read and find a way to process them. This could involve extracting text from the documents and feeding this text into the GPT-4 API request.

  2. Modify the API Request: Adjust your openai.ChatCompletion.create call to include the content of the document. You may use the documents parameter if you’re dealing with a small number of documents, or the file parameter for larger datasets. The content of these documents will then be considered by GPT-4 when generating a response.

  3. Handle Larger Documents: If your documents are long, consider breaking them into smaller chunks and sending these chunks as part of separate API calls.

  4. Ensure Sufficient Tokens: Be aware of the token limit for each API call. Larger documents or longer interactions might require managing the token count efficiently.

Remember, the implementation details will depend on the format of your documents and the specific requirements of your application. Also, ensure that you’re complying with the terms of service of the OpenAI API, especially regarding data handling and privacy.

work with MyGPT

If you see the code I attached at the beginning, it handles everything you told me, even the pdf is small and does not have many tokens.

where is your file manage part

To increase the ability to manage files with this application, you will need to add code to upload, read, and process files that users upload. This may include adding inputs to upload files on your HTML page, and file management upload in your Flask function.

Have you thought about using OpenAI Assistant, it have a knowledge retrieval feature that would do exactly what you want