|
| 1 | +# OpenAI Scala Client [](https://cequence.io) [](https://opensource.org/licenses/MIT) |
| 2 | + |
| 3 | +This is a no-nonsense async Scala client for OpenAI API supporting all the available endpoints and params (as defined [here](https://beta.openai.com/docs/api-reference)), provided in a single, convenient service called [OpenAIService](./openai-core/src/main/scala/io/cequence/openaiscala/service/OpenAIService.scala). The supported calls: |
| 4 | + |
| 5 | +* **Models**: [listModels](https://beta.openai.com/docs/api-reference/models/list), and [retrieveModel](https://beta.openai.com/docs/api-reference/models/retrieve) |
| 6 | +* **Completions**: [createCompletion](https://beta.openai.com/docs/api-reference/completions/create) |
| 7 | +* **Edits**: [createEdit](https://beta.openai.com/docs/api-reference/edits/create) |
| 8 | +* **Images**: [createImage](https://beta.openai.com/docs/api-reference/images/create), [createImageEdit](https://beta.openai.com/docs/api-reference/images/create-edit), and [createImageVariation](https://beta.openai.com/docs/api-reference/images/create-variation) |
| 9 | +* **Embeddings**: [createEmbeddings](https://beta.openai.com/docs/api-reference/embeddings/create) |
| 10 | +* **Files**: [listFiles](https://beta.openai.com/docs/api-reference/files/list), [uploadFile](https://beta.openai.com/docs/api-reference/files/upload), [deleteFile](https://beta.openai.com/docs/api-reference/files/delete), [retrieveFile](https://beta.openai.com/docs/api-reference/files/retrieve), and [retrieveFileContent](https://beta.openai.com/docs/api-reference/files/retrieve-content) |
| 11 | +* **Fine-tunes**: [createFineTune](https://beta.openai.com/docs/api-reference/fine-tunes/create), [listFineTunes](https://beta.openai.com/docs/api-reference/fine-tunes/list), [retrieveFineTune](https://beta.openai.com/docs/api-reference/fine-tunes/retrieve), [cancelFineTune](https://beta.openai.com/docs/api-reference/fine-tunes/cancel), [listFineTuneEvents](https://beta.openai.com/docs/api-reference/fine-tunes/events), and [deleteFineTuneModel](https://beta.openai.com/docs/api-reference/fine-tunes/delete-model) |
| 12 | +* **Moderations**: [createModeration](https://beta.openai.com/docs/api-reference/moderations/create) |
| 13 | + |
| 14 | +Note that in order to be consistent with the OpenAI API naming, the service's function names match exactly the API endpoint names/descriptions with camelcase. |
| 15 | +We also aimed to reduce dependencies as much as possible therefore we aimed the lib to be self-contained and use only two libs `play-ahc-ws-standalone` and `play-ahc-ws-standalone`. Additionally, if dependency injection is required we use `scala-guice` lib. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +The currently supported Scala versions are **2.12** and **2.13** but **Scala 3**-version will come out soon. |
| 20 | + |
| 21 | +To pull the library you have to add the following dependency to your *build.sbt* |
| 22 | + |
| 23 | +``` |
| 24 | +"io.cequence" %% "openai-scala-client" % "0.0.1" |
| 25 | +``` |
| 26 | + |
| 27 | +or to *pom.xml* (if you use maven) |
| 28 | + |
| 29 | +``` |
| 30 | +<dependency> |
| 31 | + <groupId>io.cequence</groupId> |
| 32 | + <artifactId>openai-scala-client_2.12</artifactId> |
| 33 | + <version>0.0.1</version> |
| 34 | +</dependency> |
| 35 | +``` |
| 36 | + |
| 37 | +## Config |
| 38 | + |
| 39 | +- Env. variables: `OPENAI_SCALA_CLIENT_API_KEY` and optionally also `OPENAI_SCALA_CLIENT_ORG_ID` (if you have one) |
| 40 | +- File config: [openai-scala-client.conf](./openai-core/src/main/resources/openai-scala-client.conf) |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +**I. Obtaining OpenAIService** |
| 45 | + |
| 46 | +First you need to provide an implicit execution context and akka materializer, e.g., as |
| 47 | + |
| 48 | +```scala |
| 49 | + implicit val ec = ExecutionContext.global |
| 50 | + implicit val materializer = Materializer(ActorSystem()) |
| 51 | +``` |
| 52 | + |
| 53 | +Then you can get a service in one of the following ways: |
| 54 | + |
| 55 | +- Default config (expects env. variables to be set as defined in `Config` section) |
| 56 | +```scala |
| 57 | + val service = OpenAIServiceFactory() |
| 58 | +``` |
| 59 | + |
| 60 | +- Custom config |
| 61 | +```scala |
| 62 | + val config = ConfigFactory.load("path_to_my_custom_config") |
| 63 | + val service = OpenAIServiceFactory(config) |
| 64 | +``` |
| 65 | + |
| 66 | +- Without config |
| 67 | + |
| 68 | +```scala |
| 69 | + val service = OpenAIServiceFactory( |
| 70 | + apiKey = "your_api_key", |
| 71 | + orgId = Some("your_org_id") // if you have one |
| 72 | + ) |
| 73 | +``` |
| 74 | + |
| 75 | +- Via dependecy injection (requires `openai-scala-guice` lib) |
| 76 | + |
| 77 | +```scala |
| 78 | + class MyClass @Inject() (openAIService: OpenAIService) {...} |
| 79 | +``` |
| 80 | + |
| 81 | +**II. Calling functions** |
| 82 | + |
| 83 | +Note that all calls are async therefore they return `Future`s. Full documentation of each call, which includes the inputs and the settings, is provided in [OpenAIService](./openai-core/src/main/scala/io/cequence/openaiscala/service/OpenAIService.scala) |
| 84 | + |
| 85 | +Examples: |
| 86 | + |
| 87 | +- List models: |
| 88 | + |
| 89 | +```scala |
| 90 | + service.listModels.map(models => |
| 91 | + models.foreach(println(_)) |
| 92 | + ) |
| 93 | +``` |
| 94 | + |
| 95 | +- Retrieve model: |
| 96 | +```scala |
| 97 | + service.retrieveModel(ModelId.text_davinci_003).map(model => |
| 98 | + println(model.getOrElse("N/A")) |
| 99 | + ) |
| 100 | +``` |
| 101 | + |
| 102 | +- Create completion: |
| 103 | +```scala |
| 104 | + val text = """Extract the name and mailing address from this email: |
| 105 | + |Dear Kelly, |
| 106 | + |It was great to talk to you at the seminar. I thought Jane's talk was quite good. |
| 107 | + |Thank you for the book. Here's my address 2111 Ash Lane, Crestview CA 92002 |
| 108 | + |Best, |
| 109 | + |Maya |
| 110 | + """.stripMargin |
| 111 | + |
| 112 | + service.createCompletion(text).map(completition => |
| 113 | + println(completition.choices.head.text) |
| 114 | + ) |
| 115 | +``` |
| 116 | + |
| 117 | +- Create completion with a custom setting: |
| 118 | + |
| 119 | +```scala |
| 120 | + val text = """Extract the name and mailing address from this email: |
| 121 | + |Dear Kelly, |
| 122 | + |It was great to talk to you at the seminar. I thought Jane's talk was quite good. |
| 123 | + |Thank you for the book. Here's my address 2111 Ash Lane, Crestview CA 92002 |
| 124 | + |Best, |
| 125 | + |Maya |
| 126 | + """.stripMargin |
| 127 | + |
| 128 | + service.createCompletion( |
| 129 | + text, |
| 130 | + settings = CreateCompletionSettings( |
| 131 | + model = ModelId.text_davinci_001, |
| 132 | + max_tokens = Some(2000), |
| 133 | + temperature = Some(0.9), |
| 134 | + presence_penalty = Some(0.2), |
| 135 | + frequency_penalty = Some(0.2) |
| 136 | + ) |
| 137 | + ).map(completition => |
| 138 | + println(completition.choices.head.text) |
| 139 | + ) |
| 140 | +``` |
| 141 | + |
| 142 | + |
| 143 | +## FAQ |
| 144 | + |
| 145 | +1. *Wen Scala 3?* |
| 146 | + |
| 147 | + Feb 2023 |
| 148 | + |
| 149 | +2. I got a timeout exception. How can I change the timeout setting? |
| 150 | + |
| 151 | + You can do it either by passing the `timeouts` param to `OpenAIServiceFactory` or if you use your own configuration file then you can set it there, such as: |
| 152 | + |
| 153 | +``` |
| 154 | +openai-scala-client { |
| 155 | + timeouts { |
| 156 | + requestTimeoutSec = 200 |
| 157 | + readTimeoutSec = 200 |
| 158 | + connectTimeoutSec = 5 |
| 159 | + pooledConnectionIdleTimeoutSec = 60 |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## License |
| 165 | + |
| 166 | +This library is available and published as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
| 167 | + |
| 168 | +## Contributors |
| 169 | + |
| 170 | +This project is open-source and welcomes any contribution or feedback ([here]()). |
| 171 | + |
| 172 | +Development of this library has been supported by [<img src="https://cequence.io/favicon-16x16.png"> - Cequence.io](https://cequence.io) - `The future of contracting` |
| 173 | + |
| 174 | +Created and maintained by [Peter Banda](https://peterbanda.net). |
0 commit comments