sbrunk / sttp-openai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sttp-model

Ideas, suggestions, problems, questions CI

sttp is a family of Scala HTTP-related projects, and currently includes:

  • sttp client: The Scala HTTP client you always wanted!
  • sttp tapir: Typed API descRiptions
  • sttp openai: this project. Scala client wrapper for OpenAI API. Use the power of ChatGPT inside your code!

Intro

Sttp-openai uses sttp client to describe requests and responses used in OpenAI endpoints.

Quickstart with sbt

Add the following dependency:

"com.softwaremill.sttp.openai" %% "core" % "0.0.6"

sttp openai is available for Scala 2.13 and Scala 3

Project content

OpenAI API Official Documentation https://platform.openai.com/docs/api-reference/completions

Not yet implemented:

* Create chat completions SSE
* Create completions SSE
* List fine-tune events SSE

Example

To use ChatGPT

import sttp.client4._
import sttp.openai.OpenAISyncClient
import sttp.openai.requests.completions.chat.ChatRequestResponseData.ChatResponse
import sttp.openai.requests.completions.chat.ChatRequestBody.{ChatBody, ChatCompletionModel}
import sttp.openai.requests.completions.chat.{Message, Role}


object Main extends App {
  // Create an instance of OpenAISyncClient providing your API secret-key
  val openAI: OpenAISyncClient = OpenAISyncClient("your-secret-key")

  // Create body of Chat Completions Request
  val bodyMessages: Seq[Message] = Seq(
    Message(
      role = Role.User,
      content = "Hello!"
    )
  )

  val chatRequestBody: ChatBody = ChatBody(
    model = ChatCompletionModel.GPT35Turbo,
    messages = bodyMessages
  )

  // be aware that calling `createChatCompletion` may throw an OpenAIException
  // e.g. AuthenticationException, RateLimitException and many more
  val chatResponse: ChatResponse = openAI.createChatCompletion(chatRequestBody)

  println(chatResponse)
}
/*
  ChatResponse(
   chatcmpl-79shQITCiqTHFlI9tgElqcbMTJCLZ,chat.completion,
   1682589572,
   gpt-3.5-turbo-0301,
   Usage(10,10,20),
   List(
     Choices(
       Message(assistant, Hello there! How can I assist you today?), stop, 0)
     )
   )
  */

Currently only two backend implementations are available:

  • OpenAISyncBackend which uses identity monad Id[A] as an effect F[A] and throws OpenAIException
  • OpenAI which provides raw sttp Requests and wraps Responses into Either[OpenAIException, A]

If you want to make use of other effects, you have to use OpenAI and pass the chosen backend directly to request.send(backend) function.

Example below uses HttpClientCatsBackend as a backend, make sure to add it to the dependencies or use backend of your choice.

import cats.effect.{ExitCode, IO, IOApp}
import sttp.client4._
import sttp.client4.httpclient.cats.HttpClientCatsBackend
import sttp.openai.OpenAIExceptions.OpenAIException
import sttp.openai._
import sttp.openai.requests.completions.chat.ChatRequestBody.{ChatBody, ChatCompletionModel}
import sttp.openai.requests.completions.chat.ChatRequestResponseData.ChatResponse
import sttp.openai.requests.completions.chat.{Message, Role}

object Main extends IOApp {
  override def run(args: List[String]): IO[ExitCode] = {
    val openAI: OpenAI = new OpenAI("your-secret-key")

    val bodyMessages: Seq[Message] = Seq(
      Message(
        role = Role.User,
        content = "Hello!"
      )
    )

    val chatRequestBody: ChatBody = ChatBody(
      model = ChatCompletionModel.GPT35Turbo,
      messages = bodyMessages
    )
    HttpClientCatsBackend.resource[IO]().use { backend =>
      val response: IO[Either[OpenAIException, ChatResponse]] =
        openAI
          .createChatCompletion(chatRequestBody)
          .send(backend)
          .map(_.body)
      val rethrownResponse: IO[ChatResponse] = response.rethrow
      val redeemedResponse: IO[String] = rethrownResponse.redeem(
        error => error.getMessage,
        chatResponse => chatResponse.toString
      )
      redeemedResponse.flatMap(IO.println)
        .as(ExitCode.Success)
    }
  }
}
/*
ChatResponse(
  chatcmpl-79shQITCiqTHFlI9tgElqcbMTJCLZ,chat.completion,
  1682589572,
  gpt-3.5-turbo-0301,
  Usage(10,10,20),
  List(
    Choices(
      Message(assistant, Hello there! How can I assist you today?), stop, 0)
    )
  )
)
*/

Contributing

If you have a question, or hit a problem, feel free to post on our community https://softwaremill.community/c/open-source/

Or, if you encounter a bug, something is unclear in the code or documentation, don’t hesitate and open an issue on GitHub.

Commercial Support

We offer commercial support for sttp and related technologies, as well as development services. Contact us to learn more about our offer!

Copyright

Copyright (C) 2023 SoftwareMill https://softwaremill.com.

About

License:Apache License 2.0


Languages

Language:Scala 100.0%