Skip to content

Commit

Permalink
add chat and audio api
Browse files Browse the repository at this point in the history
  • Loading branch information
wkok committed Mar 11, 2023
1 parent 39a23a5 commit 995336e
Show file tree
Hide file tree
Showing 10 changed files with 697 additions and 143 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 0.4.0 - 2023-03-11
### Supported API versions
- OpenAI v1.2.0
- Azure OpenAI 2022-12-01
### Changes
- Add support for Chat API
- Add support for Audio API

## 0.3.1 - 2023-02-23
### Supported API versions
- OpenAI v1.1.0
Expand Down
61 changes: 25 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ tools and applications which can work with both variants.
* [Usage - OpenAI](/doc/01-usage-openai.md)
* [Usage - Azure OpenAI](/doc/02-usage-azure.md)
* [Streaming Tokens](/doc/03-streaming.md)
* [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api)
* [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api)

## Supported APIs

| | OpenAI | Azure OpenAI |
| ----------- | :----: | :----------: |
| Version | v1.1.0 | v2022-12-01 |
| Version | v1.2.0 | v2022-12-01 |
| [Chat](https://platform.openai.com/docs/api-reference/chat) | X | |
| [Audio](https://platform.openai.com/docs/api-reference/audio) | X | |
| [Completion](https://platform.openai.com/docs/api-reference/completions) | X | X |
| [Embeddings](https://platform.openai.com/docs/api-reference/embeddings) | X | X |
| [Models](https://platform.openai.com/docs/api-reference/models) | X | |
Expand All @@ -40,13 +42,13 @@ Add the `openai-clojure` dependency
### deps.edn

```
net.clojars.wkok/openai-clojure {:mvn/version "0.3.1"}
net.clojars.wkok/openai-clojure {:mvn/version "0.4.0"}
```

### Leiningen project.clj

```
[net.clojars.wkok/openai-clojure "0.3.1"]
[net.clojars.wkok/openai-clojure "0.4.0"]
```

## Java
Expand All @@ -69,55 +71,42 @@ An API key can be generated in your [OpenAI account](https://platform.openai.com

### Azure OpenAI

#### API Key

Set the environment variable `AZURE_OPENAI_API_KEY` to your Azure OpenAI key.

#### API endpoint

Set the environment variable `AZURE_OPENAI_API_ENDPOINT` to your Azure OpenAI endpoint, example: *https://myendpoint.openai.azure.com*
See: [Authentication - Azure OpenAI](/doc/02-usage-azure.md#authentication)

## Quickstart

See the full [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api) for examples of all the supported OpenAI APIs.
See the full [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api) for examples of all the supported OpenAI APIs.

Require the `api` namespace

```
(:require [wkok.openai-clojure.api :as api])
```

A simple completion prompt for OpenAI could be:
A simple chat conversation with OpenAI's ChatGPT could be:

```
(api/create-completion {:model "text-davinci-003"
:prompt "Say this is a test"
:max_tokens 7
:temperature 0})
(api/create-chat-completion {:model "gpt-3.5-turbo"
:messages [{:role "system" :content "You are a helpful assistant."}
{:role "user" :content "Who won the world series in 2020?"}
{:role "assistant" :content "The Los Angeles Dodgers won the World Series in 2020."}
{:role "user" :content "Where was it played?"}]})
```

or for Azure OpenAI:

```
(api/create-completion :azure {:model "text-davinci-003"
:prompt "Say this is a test"
:max_tokens 7
:temperature 0})
```


Result:
```
{:id "cmpl-6jY1xInJeGGpzUgsZtkuxDsf5DdBa",
:object "text_completion",
:created 1676313593,
:model "text-davinci-003",
{:id "chatcmpl-6srOKLabYTpTRwRUQxjkcBxw3uf1H",
:object "chat.completion",
:created 1678532968,
:model "gpt-3.5-turbo-0301",
:usage {:prompt_tokens 56, :completion_tokens 19, :total_tokens 75},
:choices
[{:text "\n\nThis is indeed a test",
:index 0,
:logprobs nil,
:finish_reason "length"}],
:usage {:prompt_tokens 5, :completion_tokens 7, :total_tokens 12}}
[{:message
{:role "assistant",
:content
"The 2020 World Series was played at Globe Life Field in Arlington, Texas."},
:finish_reason "stop",
:index 0}]}
```

## Issues and features
Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:require [org.corfield.build :as bb]))

(def lib 'net.clojars.wkok/openai-clojure)
(def version "0.3.1")
(def version "0.4.0")

(defn test "Run the tests." [opts]
(bb/run-tests opts))
Expand Down
90 changes: 53 additions & 37 deletions doc/01-usage-openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Add the `openai-clojure` dependency
### deps.edn

```
net.clojars.wkok/openai-clojure {:mvn/version "0.3.1"}
net.clojars.wkok/openai-clojure {:mvn/version "0.4.0"}
```

### Leiningen project.clj

```
[net.clojars.wkok/openai-clojure "0.3.1"]
[net.clojars.wkok/openai-clojure "0.4.0"]
```

## Authentication
Expand All @@ -32,95 +32,111 @@ An API key can be generated in your [OpenAI account](https://platform.openai.com

## Quickstart

See the full [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api) api documentation for examples of all the supported OpenAI APIs.
See the full [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api) api documentation for examples of all the supported OpenAI APIs.

Require the `api` namespace

```
(:require [wkok.openai-clojure.api :as api])
```

A simple completion prompt could be:
A simple chat conversation with ChatGPT could be:

```
(api/create-completion {:model "text-davinci-003"
:prompt "Say this is a test"
:max_tokens 7
:temperature 0})
(api/create-chat-completion {:model "gpt-3.5-turbo"
:messages [{:role "system" :content "You are a helpful assistant."}
{:role "user" :content "Who won the world series in 2020?"}
{:role "assistant" :content "The Los Angeles Dodgers won the World Series in 2020."}
{:role "user" :content "Where was it played?"}]})
```

Result:
```
{:id "cmpl-6jY1xInJeGGpzUgsZtkuxDsf5DdBa",
:object "text_completion",
:created 1676313593,
:model "text-davinci-003",
{:id "chatcmpl-6srOKLabYTpTRwRUQxjkcBxw3uf1H",
:object "chat.completion",
:created 1678532968,
:model "gpt-3.5-turbo-0301",
:usage {:prompt_tokens 56, :completion_tokens 19, :total_tokens 75},
:choices
[{:text "\n\nThis is indeed a test",
:index 0,
:logprobs nil,
:finish_reason "length"}],
:usage {:prompt_tokens 5, :completion_tokens 7, :total_tokens 12}}
[{:message
{:role "assistant",
:content
"The 2020 World Series was played at Globe Life Field in Arlington, Texas."},
:finish_reason "stop",
:index 0}]}
```

## Supported OpenAI APIs

### Models

* [list-models](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#list-models)
* [retrieve-model](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#retrieve-model)
* [list-models](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#list-models)
* [retrieve-model](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#retrieve-model)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/models)

### Completions

* [create-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-completion)
* [create-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-completion)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/completions)

### Chat

* [create-chat-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-chat-completion)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat)

### Edits

* [create-edit](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-edit)
* [create-edit](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-edit)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/edits)

### Images

* [create-image](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-image)
* [create-image-edit](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-image-edit)
* [create-image-variation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-image-variation)
* [create-image](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-image)
* [create-image-edit](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-image-edit)
* [create-image-variation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-image-variation)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/images)

### Embeddings

* [create-embedding](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-embedding)
* [create-embedding](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-embedding)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/embeddings)

### Audio

* [create-transcription](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-transcription)
* [create-translation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-translation)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/audio)

### Files

* [list-files](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#list-files)
* [create-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-file)
* [delete-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#delete-file)
* [retrieve-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#retrieve-file)
* [download-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#download-file)
* [list-files](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#list-files)
* [create-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-file)
* [delete-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#delete-file)
* [retrieve-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#retrieve-file)
* [download-file](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#download-file)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/files)

### Fine-tunes

* [create-fine-tune](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-fine-tune)
* [list-fine-tunes](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#list-fine-tunes)
* [retrieve-fine-tune](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#retrieve-fine-tune)
* [cancel-fine-tune](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#cancel-fine-tune)
* [list-fine-tune-events](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#list-fine-tune-events)
* [delete-model](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#delete-model)
* [create-fine-tune](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-fine-tune)
* [list-fine-tunes](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#list-fine-tunes)
* [retrieve-fine-tune](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#retrieve-fine-tune)
* [cancel-fine-tune](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#cancel-fine-tune)
* [list-fine-tune-events](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#list-fine-tune-events)
* [delete-model](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#delete-model)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/fine-tunes)

### Moderations

* [create-moderation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-moderation)
* [create-moderation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-moderation)

Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/moderations)
10 changes: 5 additions & 5 deletions doc/02-usage-azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Add the `openai-clojure` dependency
### deps.edn

```
net.clojars.wkok/openai-clojure {:mvn/version "0.3.1"}
net.clojars.wkok/openai-clojure {:mvn/version "0.4.0"}
```

### Leiningen project.clj

```
[net.clojars.wkok/openai-clojure "0.3.1"]
[net.clojars.wkok/openai-clojure "0.4.0"]
```

## Authentication
Expand All @@ -30,7 +30,7 @@ Set the environment variable `AZURE_OPENAI_API_ENDPOINT` to your [Azure OpenAPI

## Quickstart

See the full [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api) api documentation for examples of all the supported OpenAI APIs.
See the full [API Reference](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api) api documentation for examples of all the supported OpenAI APIs.

Require the `api` namespace

Expand Down Expand Up @@ -65,12 +65,12 @@ Result:

### Completions

* [create-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-completion)
* [create-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-completion)

Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#completions)

### Embeddings

* [create-embedding](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.3.1/api/wkok.openai-clojure.api#create-embedding)
* [create-embedding](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.4.0/api/wkok.openai-clojure.api#create-embedding)

Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#embeddings)
Loading

0 comments on commit 995336e

Please sign in to comment.