Skip to content

Commit 3e95f46

Browse files
committed
added documentation for customLLM.
Signed-off-by: Sahil Suman <sahilsuman933@gmail.com>
1 parent 31e6d1a commit 3e95f46

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

custom-llm-guide.mdx

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: "Connecting Your Custom LLM to Vapi: A Comprehensive Guide"
3+
sidebarTitle: "Custom LLM"
4+
---
5+
6+
This guide provides a comprehensive walkthrough on integrating Vapi with OpenAI's gpt-3.5-turbo-instruct model using a custom LLM configuration. We'll leverage Ngrok to expose a local development environment for testing and demonstrate the communication flow between Vapi and your LLM.
7+
## Prerequisites
8+
9+
- **Vapi Account**: Access to the Vapi Dashboard for configuration.
10+
- **OpenAI API Key**: With access to the gpt-3.5-turbo-instruct model.
11+
- **Python Environment**: Set up with the OpenAI library (`pip install openai`).
12+
- **Ngrok**: For exposing your local server to the internet.
13+
- **Code Reference**: Familiarize yourself with the `/openai-sse/chat/completions` endpoint function in the provided Github repository: [Server-Side Example Python Flask](https://github.com/VapiAI/server-side-example-python-flask/blob/main/app/api/custom_llm.py).
14+
15+
## Step 1: Setting Up Your Local Development Environment
16+
17+
**1. Create a Python Script (app.py):**
18+
19+
```python
20+
from flask import Flask, request, jsonify
21+
import openai
22+
23+
app = Flask(__name__)
24+
openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your actual API key
25+
26+
@app.route("/chat/completions", methods=["POST"])
27+
def chat_completions():
28+
data = request.get_json()
29+
# Extract relevant information from data (e.g., prompt, conversation history)
30+
# ...
31+
32+
response = openai.ChatCompletion.create(
33+
model="gpt-3.5-turbo-instruct",
34+
messages=[
35+
{"role": "system", "content": "You are a helpful assistant."},
36+
# ... (Add messages from conversation history and current prompt)
37+
]
38+
)
39+
# Format response according to Vapi's structure
40+
# ...
41+
return jsonify(formatted_response)
42+
43+
if __name__ == "__main__":
44+
app.run(debug=True, port=5000) # You can adjust the port if needed
45+
```
46+
**2. Run the Script:**
47+
Execute the Python script using python app.py in your terminal. This will start the Flask server on the specified port (5000 in this example).
48+
49+
**3. Expose with Ngrok:**
50+
Open a new terminal window and run ngrok http 5000 (replace 5000 with your chosen port) to create a public URL that tunnels to your local server.
51+
52+
## Step 2: Configuring Vapi with Custom LLM
53+
**1. Access Vapi Dashboard:**
54+
Log in to your Vapi account and navigate to the "Model" section.
55+
56+
**2. Select Custom LLM:**
57+
Choose the "Custom LLM" option to set up the integration.
58+
59+
**3. Enter Ngrok URL:**
60+
Paste the public URL generated by ngrok (e.g., https://your-unique-id.ngrok.io) into the endpoint field. This will be the URL Vapi uses to communicate with your local server.
61+
62+
**4. Test the Connection:**
63+
Send a test message through the Vapi interface to ensure it reaches your local server and receives a response from the OpenAI API. Verify that the response is displayed correctly in Vapi.
64+
65+
## Step 3: Understanding the Communication Flow
66+
**1. Vapi Sends POST Request:**
67+
When a user interacts with your Vapi application, Vapi sends a POST request containing conversation context and metadata to the configured endpoint (your ngrok URL).
68+
69+
**2. Local Server Processes Request:**
70+
Your Python script receives the POST request and the chat_completions function is invoked.
71+
72+
**3. Extract and Prepare Data:**
73+
The script parses the JSON data, extracts relevant information (prompt, conversation history), and builds the prompt for the OpenAI API call.
74+
75+
**4. Call to OpenAI API:**
76+
The constructed prompt is sent to the gpt-3.5-turbo-instruct model using the openai.ChatCompletion.create method.
77+
78+
**5. Receive and Format Response:**
79+
The response from OpenAI, containing the generated text, is received and formatted according to Vapi's expected structure.
80+
81+
**6. Send Response to Vapi:**
82+
The formatted response is sent back to Vapi as a JSON object.
83+
84+
**7. Vapi Displays Response:**
85+
Vapi receives the response and displays the generated text within the conversation interface to the user.
86+
87+
By following these detailed steps and understanding the communication flow, you can successfully connect Vapi to OpenAI's gpt-3.5-turbo-instruct model and create powerful conversational experiences within your Vapi applications. The provided code example and reference serve as a starting point for you to build and customize your integration based on your specific needs.

mint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@
184184
"group": "Multilingual Support",
185185
"pages": ["multilingual"]
186186
},
187-
"GHL"
187+
"GHL",
188+
"custom-llm-guide"
188189
]
189190
},
190191

0 commit comments

Comments
 (0)