Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions with-openai/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STEPZEN_OPENAI_API_KEY=sk-s6-yourkey-yourkey-yourkey-H3
5 changes: 5 additions & 0 deletions with-openai/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
yarn.lock
package-lock.json
.env
!config.yaml
46 changes: 46 additions & 0 deletions with-openai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# StepZen Example: `with-openai`

## Introduction

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)

## Getting Started

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:

```bash
git clone https://github.com/stepzen-dev/examples.git
cd examples/with-openai
```

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:

```bash
echo "STEPZEN_OPENAI_API_KEY=sk-s6-yourkey-yourkey-yourkey-H3" > .env
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typically I add a .env.sample file too

```

## Run StepZen

Open your terminal and [install the StepZen CLI](https://stepzen.com/docs/quick-start):

```bash
npm install -g stepzen
```

You need to login here using the command:

```bash
stepzen login
```

After you've installed the CLI and logged in, run:

```bash
stepzen start
```

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.

## Learn More

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.
4 changes: 4 additions & 0 deletions with-openai/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
configurationset:
- configuration:
name: openai_config
openai_auth: Bearer STEPZEN_OPENAI_API_KEY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rename to OPENAI_API_KEY to prevent confusion with the stepzen key

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to use an env var in config.yaml it has to start with STEPZEN_ - the CLI only substitutes such vars.
If starting with STEPZEN_ is a problem, then we should consider dropping / changing this CLI feature.

3 changes: 3 additions & 0 deletions with-openai/index.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema @sdl(files: ["openai/image-generations.graphql"]) {
query: Query
}
18 changes: 18 additions & 0 deletions with-openai/openai/image-generations.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type ImageData {
url: String
}

type GenerationResult {
created: Int
data: [ImageData]
}

type Query {
generations(prompt: String!, n: Int, size: String): GenerationResult
@rest(
method: POST
endpoint: "https://api.openai.com/v1/images/generations"
headers: [{ name: "Authorization", value: "$openai_auth;" }]
configuration: "openai_config"
)
}
3 changes: 3 additions & 0 deletions with-openai/stepzen.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endpoint": "api/with-openai"
}