|
1 | | -# Website |
| 1 | +# `openai` provider for [`stackql`](https://github.com/stackql/stackql) |
2 | 2 |
|
3 | | -This website is built using [Docusaurus](https://docusaurus.io/), a modern static website generator. |
| 3 | +This repository is used to generate and document the `openai` provider for StackQL, allowing you to query and interact with OpenAI services using SQL-like syntax. The provider is built using the `@stackql/provider-utils` package, which provides tools for converting OpenAPI specifications into StackQL-compatible provider schemas. |
4 | 4 |
|
5 | | -## Installation |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +To use the OpenAI provider with StackQL, you'll need: |
| 8 | + |
| 9 | +1. An OpenAI account with appropriate API credentials |
| 10 | +2. An OpenAI API key with sufficient permissions for the resources you want to access |
| 11 | +3. StackQL CLI installed on your system (see [StackQL](https://github.com/stackql/stackql)) |
| 12 | + |
| 13 | +## 1. Download the Open API Specification |
| 14 | + |
| 15 | +First, download or create the OpenAI API OpenAPI specification: |
6 | 16 |
|
7 | 17 | ```bash |
8 | | -yarn |
| 18 | +mkdir -p provider-dev/downloaded |
| 19 | +curl -L https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml \ |
| 20 | + -o provider-dev/downloaded/openai-openapi.yaml |
| 21 | + |
| 22 | +# Convert YAML to JSON if needed |
| 23 | +python3 provider-dev/scripts/yaml_to_json.py \ |
| 24 | + --input provider-dev/downloaded/openai-openapi.yaml \ |
| 25 | + --output provider-dev/downloaded/openapi.json |
9 | 26 | ``` |
10 | 27 |
|
11 | | -## Local Development |
| 28 | +## 2. Split into Service Specs |
| 29 | + |
| 30 | +Next, split the OpenAPI specification into service-specific files: |
12 | 31 |
|
13 | 32 | ```bash |
14 | | -yarn start |
| 33 | +rm -rf provider-dev/source/* |
| 34 | +npm run split -- \ |
| 35 | + --provider-name openai \ |
| 36 | + --api-doc provider-dev/downloaded/openapi.json \ |
| 37 | + --svc-discriminator tag \ |
| 38 | + --output-dir provider-dev/source \ |
| 39 | + --overwrite \ |
| 40 | + --svc-name-overrides "$(cat <<EOF |
| 41 | +{ |
| 42 | + "models": "models", |
| 43 | + "completions": "completions", |
| 44 | + "chat": "chat", |
| 45 | + "embeddings": "embeddings", |
| 46 | + "fine-tuning": "fine_tuning", |
| 47 | + "files": "files", |
| 48 | + "images": "images", |
| 49 | + "audio": "audio", |
| 50 | + "moderations": "moderations", |
| 51 | + "assistants": "assistants", |
| 52 | + "threads": "threads", |
| 53 | + "batches": "batches" |
| 54 | +} |
| 55 | +EOF |
| 56 | +)" |
15 | 57 | ``` |
16 | 58 |
|
17 | | -This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server. |
| 59 | +## 3. Generate Mappings |
18 | 60 |
|
19 | | -## Build |
| 61 | +Generate the mapping configuration that connects OpenAPI operations to StackQL resources: |
20 | 62 |
|
21 | 63 | ```bash |
22 | | -yarn build |
| 64 | +npm run generate-mappings -- \ |
| 65 | + --provider-name openai \ |
| 66 | + --input-dir provider-dev/source \ |
| 67 | + --output-dir provider-dev/config |
| 68 | +``` |
| 69 | + |
| 70 | +Update the resultant `provider-dev/config/all_services.csv` to add the `stackql_resource_name`, `stackql_method_name`, `stackql_verb` values for each operation. |
| 71 | + |
| 72 | +## 4. Generate Provider |
| 73 | + |
| 74 | +This step transforms the split OpenAPI service specs into a fully-functional StackQL provider by applying the resource and method mappings defined in your CSV file. |
| 75 | + |
| 76 | +```bash |
| 77 | +rm -rf provider-dev/openapi/* |
| 78 | +npm run generate-provider -- \ |
| 79 | + --provider-name openai \ |
| 80 | + --input-dir provider-dev/source \ |
| 81 | + --output-dir provider-dev/openapi/src/openai \ |
| 82 | + --config-path provider-dev/config/all_services.csv \ |
| 83 | + --servers '[{"url": "https://api.openai.com/v1"}]' \ |
| 84 | + --provider-config '{"auth": {"credentialsenvvar": "OPENAI_API_KEY", "type": "bearer"}}' \ |
| 85 | + --overwrite |
23 | 86 | ``` |
24 | 87 |
|
25 | | -This command generates static content into the `build` directory and can be served using any static contents hosting service. |
| 88 | +## 5. Test Provider |
26 | 89 |
|
27 | | -## Deployment |
| 90 | +### Starting the StackQL Server |
28 | 91 |
|
29 | | -Using SSH: |
| 92 | +Before running tests, start a StackQL server with your provider: |
30 | 93 |
|
31 | 94 | ```bash |
32 | | -USE_SSH=true yarn deploy |
| 95 | +PROVIDER_REGISTRY_ROOT_DIR="$(pwd)/provider-dev/openapi" |
| 96 | +npm run start-server -- --provider openai --registry $PROVIDER_REGISTRY_ROOT_DIR |
33 | 97 | ``` |
34 | 98 |
|
35 | | -Not using SSH: |
| 99 | +### Test Meta Routes |
| 100 | + |
| 101 | +Test all metadata routes (services, resources, methods) in the provider: |
36 | 102 |
|
37 | 103 | ```bash |
38 | | -GIT_USER=<Your GitHub username> yarn deploy |
| 104 | +npm run test-meta-routes -- openai --verbose |
39 | 105 | ``` |
40 | 106 |
|
41 | | -If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. |
| 107 | +When you're done testing, stop the StackQL server: |
| 108 | + |
| 109 | +```bash |
| 110 | +npm run stop-server |
| 111 | +``` |
| 112 | + |
| 113 | +Use this command to view the server status: |
| 114 | + |
| 115 | +```bash |
| 116 | +npm run server-status |
| 117 | +``` |
| 118 | + |
| 119 | +### Run test queries |
| 120 | + |
| 121 | +Run some test queries against the provider using the `stackql shell`: |
| 122 | + |
| 123 | +```bash |
| 124 | +PROVIDER_REGISTRY_ROOT_DIR="$(pwd)/provider-dev/openapi" |
| 125 | +REG_STR='{"url": "file://'${PROVIDER_REGISTRY_ROOT_DIR}'", "localDocRoot": "'${PROVIDER_REGISTRY_ROOT_DIR}'", "verifyConfig": {"nopVerify": true}}' |
| 126 | +./stackql shell --registry="${REG_STR}" |
| 127 | +``` |
| 128 | + |
| 129 | +Example queries to try: |
| 130 | + |
| 131 | +```sql |
| 132 | +-- List all available models |
| 133 | +SELECT |
| 134 | +id, |
| 135 | +object, |
| 136 | +created, |
| 137 | +owned_by |
| 138 | +FROM openai.models.models; |
| 139 | + |
| 140 | +-- Get information about a specific model |
| 141 | +SELECT |
| 142 | +id, |
| 143 | +object, |
| 144 | +created, |
| 145 | +owned_by |
| 146 | +FROM openai.models.model |
| 147 | +WHERE id = 'gpt-4'; |
| 148 | + |
| 149 | +-- List uploaded files |
| 150 | +SELECT |
| 151 | +id, |
| 152 | +object, |
| 153 | +bytes, |
| 154 | +created_at, |
| 155 | +filename, |
| 156 | +purpose |
| 157 | +FROM openai.files.files; |
| 158 | + |
| 159 | +-- Create a chat completion (text generation) |
| 160 | +INSERT INTO openai.chat.completions ( |
| 161 | + json_data |
| 162 | +) VALUES ( |
| 163 | + '{ |
| 164 | + "model": "gpt-3.5-turbo", |
| 165 | + "messages": [ |
| 166 | + { |
| 167 | + "role": "system", |
| 168 | + "content": "You are a helpful assistant." |
| 169 | + }, |
| 170 | + { |
| 171 | + "role": "user", |
| 172 | + "content": "Hello, how are you today?" |
| 173 | + } |
| 174 | + ] |
| 175 | + }' |
| 176 | +); |
| 177 | + |
| 178 | +-- Create an embedding |
| 179 | +INSERT INTO openai.embeddings.embeddings ( |
| 180 | + json_data |
| 181 | +) VALUES ( |
| 182 | + '{ |
| 183 | + "model": "text-embedding-ada-002", |
| 184 | + "input": "The food was delicious and the service was excellent." |
| 185 | + }' |
| 186 | +); |
| 187 | + |
| 188 | +-- List fine-tuning jobs |
| 189 | +SELECT |
| 190 | +id, |
| 191 | +object, |
| 192 | +model, |
| 193 | +created_at, |
| 194 | +finished_at, |
| 195 | +fine_tuned_model, |
| 196 | +organization_id, |
| 197 | +status, |
| 198 | +training_file |
| 199 | +FROM openai.fine_tuning.jobs; |
| 200 | + |
| 201 | +-- List assistants |
| 202 | +SELECT |
| 203 | +id, |
| 204 | +object, |
| 205 | +created_at, |
| 206 | +name, |
| 207 | +description, |
| 208 | +model, |
| 209 | +instructions, |
| 210 | +tools, |
| 211 | +metadata |
| 212 | +FROM openai.assistants.assistants; |
| 213 | +``` |
| 214 | + |
| 215 | +## 6. Publish the provider |
| 216 | + |
| 217 | +To publish the provider push the `openai` dir to `providers/src` in a feature branch of the [`stackql-provider-registry`](https://github.com/stackql/stackql-provider-registry). Follow the [registry release flow](https://github.com/stackql/stackql-provider-registry/blob/dev/docs/build-and-deployment.md). |
| 218 | + |
| 219 | +Launch the StackQL shell: |
| 220 | + |
| 221 | +```bash |
| 222 | +export DEV_REG="{ \"url\": \"https://registry-dev.stackql.app/providers\" }" |
| 223 | +./stackql --registry="${DEV_REG}" shell |
| 224 | +``` |
| 225 | + |
| 226 | +Pull the latest dev `openai` provider: |
| 227 | + |
| 228 | +```sql |
| 229 | +registry pull openai; |
| 230 | +``` |
| 231 | + |
| 232 | +Run some test queries to verify the provider works as expected. |
| 233 | + |
| 234 | +## 7. Generate web docs |
| 235 | + |
| 236 | +Provider doc microsites are built using Docusaurus and published using GitHub Pages. |
| 237 | + |
| 238 | +a. Update `headerContent1.txt` and `headerContent2.txt` accordingly in `provider-dev/docgen/provider-data/` |
| 239 | + |
| 240 | +b. Update the following in `website/docusaurus.config.js`: |
| 241 | + |
| 242 | +```js |
| 243 | +// Provider configuration - change these for different providers |
| 244 | +const providerName = "openai"; |
| 245 | +const providerTitle = "OpenAI Provider"; |
| 246 | +``` |
| 247 | + |
| 248 | +c. Then generate docs using... |
| 249 | + |
| 250 | +```bash |
| 251 | +npm run generate-docs -- \ |
| 252 | + --provider-name openai \ |
| 253 | + --provider-dir ./provider-dev/openapi/src/openai/v00.00.00000 \ |
| 254 | + --output-dir ./website \ |
| 255 | + --provider-data-dir ./provider-dev/docgen/provider-data |
| 256 | +``` |
| 257 | + |
| 258 | +## 8. Test web docs locally |
| 259 | + |
| 260 | +```bash |
| 261 | +cd website |
| 262 | +# test build |
| 263 | +yarn build |
| 264 | + |
| 265 | +# run local dev server |
| 266 | +yarn start |
| 267 | +``` |
| 268 | + |
| 269 | +## 9. Publish web docs to GitHub Pages |
| 270 | + |
| 271 | +Under __Pages__ in the repository, in the __Build and deployment__ section select __GitHub Actions__ as the __Source__. In Netlify DNS create the following records: |
| 272 | + |
| 273 | +| Source Domain | Record Type | Target | |
| 274 | +|---------------|--------------|--------| |
| 275 | +| openai-provider.stackql.io | CNAME | stackql.github.io. | |
| 276 | + |
| 277 | +## License |
| 278 | + |
| 279 | +MIT |
| 280 | + |
| 281 | +## Contributing |
| 282 | + |
| 283 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments