To use a custom base URL with the official OpenAI libraries, such as when routing requests through the Func Runner proxy, you can modify the base URL configuration in each language’s library settings. Below are instructions for Python, Ruby, JavaScript, Go, and Java.
Important Note - Regarding OpenAI API Keys
Func Runner acts as a proxy service between OpenAI and your applications. Whether it’s a JavaScript application running in a browser or a Ruby process running on an AWS EC2 instance, you should treat your applications as a client service of Func Runner.
You do not need to provide a real OpenAI API key with these applications when calling Func Runner because Func Runner uses a real OpenAI key that it retrieves from the Azure Function App environment settings. It is recommended that you avoid using a real OpenAI API key in these client services unless you plan to bypass the Func Runner proxy entirely.
I have noticed that with some languages, like JavaScript, you need to include some sort of value for the apiKey field; otherwise, the client will fail to construct. In such cases, you can use dummy data because it doesn’t matter—Func Runner will use the correct key when calling OpenAI.
Python
To change the base URL in the Python OpenAI library, set the api_base
attribute:
import openai
openai.api_base = "https://<functionAppName>.azurewebsites.net/api/openai"
Ruby
In Ruby, you can modify the base URL by setting the api_base
attribute of the OpenAI::Client
:
require "openai"
client = OpenAI::Client.new(
api_base: "https://<functionAppName>.azurewebsites.net/api/openai",
api_key: "your-api-key"
)
JavaScript
For JavaScript (Node.js), adjust the basePath
property when configuring the OpenAI client:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "your-api-key",
basePath: "https://<functionAppName>.azurewebsites.net/api/openai",
});
const openai = new OpenAIApi(configuration);
Go
In Go, set the base URL using the BaseURL
field when creating a new OpenAI client:
Important Note - Unofficial OpenAI Library
sashabaranov/go-openai
is not an official OpenAI library but is recommended by OpenAI in their documentation.
package main
import (
openai "github.com/sashabaranov/go-openai"
)
func main() {
client := openai.NewClient("your-api-key")
client.BaseURL = "https://<functionAppName>.azurewebsites.net/api/openai"
}
Java
For Java, the base URL can be set when constructing the client with a custom baseUrl
configuration:
Important Note - Unofficial OpenAI Library
com.theokanning.openai
is not an official OpenAI library but is recommended by OpenAI in their documentation.
import com.theokanning.openai.service.OpenAiService;
public class OpenAIExample {
public static void main(String[] args) {
String apiKey = "your-api-key";
String baseUrl = "https://<functionAppName>.azurewebsites.net/api/openai";
OpenAiService service = new OpenAiService(apiKey, baseUrl);
}
}
These instructions show how to set a custom base URL for various OpenAI libraries, allowing you to direct API requests through your Function App endpoint. Replace <functionAppName>
with the actual name of your Azure Function App to reroute requests as needed.