Output 's differences between Open AI Assistant Playground and the same Assistant hosted on Replit

Hi, I build an Assistant on Replit, and it works, but unfortunately it has hallucinations or doesn’t find the answers for several topics. The code, hosted on Replit, is linked to the specific Assistant ID, and when I try it on the OpenAI Playground, the Assistant responds properly to every questions, it hasn’t hallucination and find all the info contained in the knowledge bases.

The knowledge base is divided in 5 files (each file with max 6k chars, so not long ones) and the funny fact is that the Assistant responds correctly to questions contained in a file, and uncorrectly for other questions in the same file. Looks like it can retrieve some info and can’t for others

So I basically need that the code hosted on Replit retrieves the knowledge base’s files as well as the Playground Assistant

How can I obtain it? If someone knows how can I obtain the same performance of the Playground Assistant, it will save my life after weeks of struggling

I paste the code of my Assistant:

from time import sleep
from flask import Flask, request, jsonify, render_template, abort
import openai
import os

# Connect to OpenAI API
openai_api_key = os.environ.get('OPENAI_API_KEY')
assistant_id = os.environ.get('ASSISTANT_ID')
if not openai_api_key or not assistant_id:
    raise EnvironmentError("OPENAI_API_KEY or ASSISTANT_ID is not set in environment variables.")

client = openai.OpenAI(api_key=openai_api_key)

# Start the Flask App
app = Flask(__name__)

def index():
    return render_template('index.html')

# Custom error handler
def internal_error(error):
    return jsonify({"error": "Internal server error"}), 500

# Function to create and return file ID from OpenAI
def create_file_for_openai(filename):
        file = client.files.create(file=open(filename, "rb"), purpose='assistants')
        return file.id
    except Exception as e:
        print(f"Error creating file '{filename}': {e}")

# For setting up a new conversation
@app.route('/start', methods=['GET'])
def initiate_conversation():
        thread = client.beta.threads.create()
        return jsonify({"thread_id": thread.id})
    except Exception as e:
        print(f"Error initiating conversation: {e}")

@app.route('/chat', methods=['POST'])
def chat():
        data = request.json
        thread_id = data.get('thread_id')
        user_input = data.get('message', '')

        # Send the user message to the thread
        client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
        # Trigger a run with the assistant
        run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)

        while run.status not in ['completed', 'failed']:
            run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)

        messages = client.beta.threads.messages.list(thread_id=thread_id, order="desc")
        # Filter messages to get the latest bot response
        bot_responses = [msg for msg in messages.data if msg.role != 'user']
        response = bot_responses[0].content[0].text.value if bot_responses else "No response found."
        return jsonify({"response": response})
    except Exception as e:
        print(f"Error during chat: {e}")

# Start the server
if __name__ == '__main__':
    app.run(host='', port=8080, debug=True)

Hope someone can help me, I will be foverer grateful