Giter VIP home page Giter VIP logo

openai-scala's Introduction

OpenAI Scala Client

The OpenAI Scala Client is a library that provides a Scala interface to the OpenAI API, allowing you to programmatically interact with OpenAI's artificial intelligence models. The library uses cats for generic monads and sttp for http client abstraction.

Installation

Currently supported Scala versions are 2.12, 2.13 and 3.
To install the OpenAI Scala Client, add the following dependency to your project's build.sbt file:

"dev.maxmelnyk" %% "openai-scala" % "0.3.0"

Getting Started

Before using the OpenAI Scala Client, you will need to obtain an OpenAI API key and (optionally) an organization ID as described in the OpenAI API documentation. Once you have obtained your API key and organization ID, you can specify them using environment variables. Set the OPENAI_API_KEY and (optionally) OPENAI_ORG_ID environment variables to your API key and organization ID respectively.
Alternatively, you can explicitly specify your API key and organization ID when creating a new OpenAI client object. See the example below:

import dev.maxmelnyk.openaiscala.client.OpenAIClient

// with organization id
val client = OpenAIClient("<your-api-key>", Some("<your-org-id>"))(sttpBackend)

// without organization id
val client = OpenAIClient("<your-api-key>")(sttpBackend)

// api key and org id taken from env vars
val client = OpenAIClient(sttpBackend)

In the following sections, we assume that the OPENAI_API_KEY and (optionally) OPENAI_ORG_ID environment variables have been set up correctly.

Usage

Using the OpenAI Scala Client is straightforward if you're already using sttp in your project and have a sttpBackend set up. Here's an example:

import dev.maxmelnyk.openaiscala.client.OpenAIClient

val sttpBackend = ???

val client = OpenAIClient(sttpBackend)

Please note that the cats's MonadThrow[F] for the monad F used in sttpBackend must be implicitly in scope.

Example

As a simple example lets use cats effect IO monad and http4s as sttp backend.

First, make sure you have the following dependencies:

// cats effect
"org.typelevel" %% "cats-effect" % "<cats_effect_version>"

// http4s client
"org.http4s" %% "http4s-dsl" % "<http4s_version>"
"org.http4s" %% "http4s-blaze-client" % "<http4s_version>"

// sttp adapter for http4s
"com.softwaremill.sttp.client3" %% "http4s-backend" % "<sttp_version>"

Then, in your code:

import cats.effect.{IO, IOApp}
import dev.maxmelnyk.openaiscala.client.OpenAIClient
import dev.maxmelnyk.openaiscala.models.models.Models
import dev.maxmelnyk.openaiscala.models.text.completions.chat.ChatCompletion
import sttp.client3.http4s.Http4sBackend

import java.io.File

object Main extends IOApp.Simple {
  val run: IO[Unit] = {
    val sttpBackendResource = Http4sBackend.usingDefaultBlazeClientBuilder[IO]()

    sttpBackendResource.use { sttpBackend =>
      val client = OpenAIClient(sttpBackend)

      for {
        // models api
        models <- client.listModels
        davinciModel <- client.retrieveModel(Models.davinci)

        // text apis
        completion <- client.createCompletion(Seq("This is a test."))
        chatCompletion <- client.createChatCompletion(Seq(ChatCompletion.Message(ChatCompletion.Message.Role.User, "Hello!")))
        edit <- client.createEdit("What day of the wek is it?", "Fix the spelling mistakes")

        // images api
        sourceImage = new File("/path/to/file/image.png")
        mask = new File("/path/to/file/mask.png")

        image <- client.createImage("A cute baby sea otter")
        editedImage <- client.createImageEdit(sourceImage, Some(mask), "A cute baby sea otter wearing a beret")
        variedImage <- client.createImageVariation(sourceImage)
      } yield {
        println(models)
        println(davinciModel)
        println(completion)
        println(chatCompletion)
        println(edit)
        println(image)
        println(editedImage)
        println(variedImage)
      }
    }
  }
}

Please note that this is just an example, and you can use any compatible monad and sttp backend with the OpenAI Scala Client.
This example can be found here, more examples can be found in the examples directory.

License

This project is licensed under the MIT License - see the LICENSE file for details.

openai-scala's People

Contributors

imaxmelnyk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.