Skip to content

Commit 79ac64a

Browse files
authored
Merge pull request #67 from stepzen-dev/feat/add-openai
add a simple OpenAI example (`images/generations`)
2 parents 347abcd + 5abfdf2 commit 79ac64a

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

with-openai/.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STEPZEN_OPENAI_API_KEY=sk-s6-yourkey-yourkey-yourkey-H3

with-openai/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
yarn.lock
3+
package-lock.json
4+
.env
5+
!config.yaml

with-openai/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# StepZen Example: `with-openai`
2+
3+
## Introduction
4+
5+
This project shows how to create a GraphQL API connected for OpenAI using StepZen. We will use the `@rest` directive to generate our GraphQL schema. You can refer to the documentation [here](https://www.openai.com/en/api/documentation)
6+
7+
## Getting Started
8+
9+
You'll need to create a [StepZen account](https://stepzen.com/request-invite) first. Once you've got that set up, [git clone](https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone) this repository onto your machine and open the working directory:
10+
11+
```bash
12+
git clone https://github.com/stepzen-dev/examples.git
13+
cd examples/with-openai
14+
```
15+
16+
You also need an OpenAI account and an API key. You can get one from the [OpenAPI web site](https://beta.openai.com/account/api-keys). Once you have it, please create a `.env` file and save it there:
17+
18+
```bash
19+
echo "STEPZEN_OPENAI_API_KEY=sk-s6-yourkey-yourkey-yourkey-H3" > .env
20+
```
21+
22+
## Run StepZen
23+
24+
Open your terminal and [install the StepZen CLI](https://stepzen.com/docs/quick-start):
25+
26+
```bash
27+
npm install -g stepzen
28+
```
29+
30+
You need to login here using the command:
31+
32+
```bash
33+
stepzen login
34+
```
35+
36+
After you've installed the CLI and logged in, run:
37+
38+
```bash
39+
stepzen start
40+
```
41+
42+
A proxy of the GraphiQL playground becomes available at `http://localhost/5001` (in example `http://localhost:5001/api/with-openai`), which you can use to explore the GraphQL API. Also, the endpoint at which your GraphQL API is deployed gets logged in the terminal. You can query your GraphQL API from any application, browser, or IDE by providing the API Key linked to your account.
43+
44+
## Learn More
45+
46+
You can learn more in the [StepZen documentation for REST](https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service). Questions? Head over to [Discord](https://discord.gg/9k2VdPn2FR) or [GitHub Discussions](https://github.com/stepzen-dev/examples/discussions) to ask questions.

with-openai/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
configurationset:
2+
- configuration:
3+
name: openai_config
4+
openai_auth: Bearer STEPZEN_OPENAI_API_KEY

with-openai/index.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
schema @sdl(files: ["openai/image-generations.graphql"]) {
2+
query: Query
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type ImageData {
2+
url: String
3+
}
4+
5+
type GenerationResult {
6+
created: Int
7+
data: [ImageData]
8+
}
9+
10+
type Query {
11+
generations(prompt: String!, n: Int, size: String): GenerationResult
12+
@rest(
13+
method: POST
14+
endpoint: "https://api.openai.com/v1/images/generations"
15+
headers: [{ name: "Authorization", value: "$openai_auth;" }]
16+
configuration: "openai_config"
17+
)
18+
}

with-openai/stepzen.config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"endpoint": "api/with-openai"
3+
}

0 commit comments

Comments
 (0)