Follow the steps in the vapi docs, then I used thi...
# general-english
j
Follow the steps in the vapi docs, then I used this as the code which worked
Copy code
from flask import Flask, request, Response, jsonify
from openai import OpenAI
import os

app = Flask(__name__)
client = OpenAI(api_key="##########################")  # Replace with your actual API key

def generate_streaming_response(data):
    """
    Generator function to stream data.
    """
    for chunk in data:
        json_data = chunk.model_dump_json()
        yield f"data: {json_data}\n\n"

@app.route("/chat/completions", methods=["POST"])
def chat_completions():
    request_data = request.get_json()
    
    # Extract relevant information from the request data
    messages = request_data.get("messages", [])
    
    streaming = request_data.get('stream', True)  # Default to streaming
    
    if streaming:
        chat_completion_stream = client.chat.completions.create(
            model="###############################",      # Replace with your actual model ID
            messages=messages,
            stream=True
        )
        return Response(generate_streaming_response(chat_completion_stream), content_type='text/event-stream')
    else:
        chat_completion = client.chat.completions.create(
            model="gpt-3.5-turbo-16k",
            messages=messages,
            stream=False
        )
        return jsonify(chat_completion.model_dump_json())

if __name__ == "__main__":
    app.run(debug=True, port=5000)