OpenAI Connector
Learn how to integrate OpenAI's models into your graph
OpenAI provides AI models like GPT-4 for text generation, completion, and processing tasks. The prebuilt OpenAI Connector provides a GraphQL interface for OpenAI's models and services, allowing you to integrate OpenAI's capabilities into your federated graph.
Connector capabilities
This Connector can incorporate the following OpenAI APIs into your graph:
Prerequisites
To use the Connector, you need an OpenAI API key.
Try out the Connector
Use the Apollo Sandbox below to test out the Connector. This is the same interface you use locally to test your queries.
To use this sandbox you either need to add your OpenAI key as a header or copy and paste placeholder values.
Add your OpenAI API key
- In the sandbox, below the Operations panel, open the Headers tab then click Add a new header.
- Add a new header with the name
Bearer
and your OpenAI token value. - Select and run a query from the Documentation tab to test.
Use placeholder values
- Copy and paste the following into the Operations panel in Sandbox:GraphQL
mutation GenerateStory($model: ID!, $about: [MessageInput]!) { createChatCompletion(model: $model, messages: $about) { id model messages { content role } usage { totalTokens promptTokens completionTokens } createdAt } }
- Copy and paste the following into the Variables panel below Operations:JSON
{ "about": [ { "role": "USER", "content": "The story about Apollo" } ], "model": "gpt-4o-mini" }
- Run the operation.
Getting started
If you haven't already, create a new graph in GraphOS.
Copy the
supergraph.yaml
androuter.yaml
files from the Connector to replace the files created by runningrover init
.Set your OpenAI API key as an environment variable in your terminal:
terminalexport OPENAI_API_KEY=....
Run
rover dev
to start the local development session:terminalAPOLLO_KEY=service:My-Graph-s1ff1u:•••••••••••••••••••••• \ APOLLO_GRAPH_REF=My-Graph-s1ff1u@main \ rover dev --supergraph-config supergraph.yaml --router-config router.yaml
You're all set! Open http://localhost:4000
to query your graph using Apollo Sandbox.
Adding to an existing graph in GraphOS
To add this Connector to an existing graph, publish the schema openai.graphql
file to your graph ref using rover subgraph publish
:
APOLLO_KEY=service:My-Graph-s1ff1u:•••••••••••••••••••••• \
rover subgraph publish My-Graph-s1ff1u@main --name openai --schema openai.graphql --routing-url http://openai
Setup VS Code task runner
Once you've set up the VS Code task runner, you can execute the Tasks: Run Task
command in VS Code to run the rover dev
task.
Edit your .vscode/settings.json
to include the following OpenAI-specific key:
{
"terminal.integrated.profiles.osx": {
"graphos": {
"path": "zsh",
"args": ["-l"],
"env": {
"OPENAI_API_KEY": "",
...
}
}
},
"terminal.integrated.defaultProfile.osx": "graphos"
}
Alternatively, you can open a new terminal window in VS Code with the graphos
profile, then run rover dev --supergraph-config supergraph.yaml --router-config router.yaml
.
Contributing
The Connectors Community welcomes contributions to this Connector or to expand the library. For instructions on how to contribute, see the contributing guide.
Modules to contribute
The following features can be added to this Connector:
Support for all OpenAI models and endpoints
Stream response handling
Function calling support
Image generation and analysis
See the OpenAI API Documentation for other features to implement.