What are your thoughts about Assistant API v2? For a noob dev

Hey everyone, I was really excited during the initial days of the Assistant API launch last November, as I believed it could be a game-changer for a novice developer like myself looking to build my first AI-supported bot. Unfortunately, after numerous tests and discussions in this forum, I realized that at that time the Assistant API wasn’t perfect. A simple assistant using just the retrieval function often produced inconsistent quality and was prone to hallucinations.

Eventually, I built a custom solution using LangChain and Qdrant, which I still use today because the quality is satisfactory. However, I’ve always dreamed of building an efficient assistant using the Assistant API.

So, I was thrilled to hear about the release of v2 today! I am eager to learn from those more experienced than myself: Is v2 truly a game-changer as described? Is it feasible to build a business around it, focusing solely on assistants with an efficient retrieval function?

I’d love to hear your thoughts on v2.

As an example, here’s a template of a Flask Application with Assistant API v1 that I built but eventually abandoned due to dissatisfaction with the quality of the outputs:

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__)

@app.route('/')
def index():
    return render_template('index.html')

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

# Function to check if a file is already associated with the assistant
def is_file_associated(assistant_id, file_id):
    try:
        associated_files = client.beta.assistants.files.list(assistant_id=assistant_id)
        for file in associated_files.data:
            if file.id == file_id:
                return True
        return False
    except Exception as e:
        print(f"Error checking file association for file ID '{file_id}': {e}")
        abort(500)

# Function to associate file with an assistant
def associate_file_with_assistant(filename, assistant_id, file_id):
    if not is_file_associated(assistant_id, file_id):
        try:
            print(f"Associating file '{filename}' with ID '{file_id}' to the assistant.")
            client.beta.assistants.files.create(assistant_id=assistant_id, file_id=file_id)
        except Exception as e:
            print(f"Error associating file '{filename}' with assistant: {e}")
            abort(500)
    else:
        print(f"File '{filename}' with ID '{file_id}' is already associated with the assistant.")

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

@app.route('/chat', methods=['POST'])
def chat():
    try:
        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']:
            sleep(1)
            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}")
        abort(500)

# Map of filenames to their corresponding file IDs
file_id_mapping = {
    'file1.json': 'file-2WWdkF5Wc9FgpdpD3hJmLl5M',
    'file2.json': 'file-Gx78kdoYdsjA7pZ1hyNrKBtK',
    'file3.json': 'file-HFbSBJfkDKi62RHOT0euUpy2',
    'file4.json': 'file-4uKFq8v4T8mkaMkO8epHdx9N',
    'file5.json': 'file-YW89scjSKWIUjG43lKktOyRK',
}

# Associate knowledge base files with the assistant at server initialization
def associate_knowledge_base_files():
    for filename, file_id in file_id_mapping.items():
        associate_file_with_assistant(filename, assistant_id, file_id)

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

I would like to test the Assistant API v2. Will I need to modify the code in any way?

Thanks in advance to everyone who participates in the discussion.

Not a Python dev but I’ve been creating the Assistants in Apex. V2 surely is giving me more improved responses in comparison to V1. OpenAI still needs to work on unsupported files issue while uploading files, apart from this I think I love V2.