Code Monkey home page Code Monkey logo

openaijava's Introduction

OpenAIJava: Java OpenAI API

OpenAIJava is a lightweight, highly modular, reactive, type safe Java and Spring Library for working with OpenAI API.

This allows you to work with the OpenAI models, without the additional overhead of having to write your own integration code for the platform.

Features

  • Complete Implementation of the 20 OpenAI endpoints
  • Implementation of request objects to easily create requests without interacting with JSON objects at all
  • Implementation of response objects to easily interact with API responses.
  • Comprehensive integration tests demonstrating a number of the above scenarios

Pending

  • Utils library with some frequent used constant variables. For example, the list of available models
  • CreateImageEditRequest and CreateImageVariationRequest contains parameter response_format which should be renamed to match Java standards. (JsonProperty won't work since POJO isn't sent as json)

It has four runtime dependencies:

  • OpenFeign for its reactive-functional API
  • OKHttp for HTTP connections
  • Jackson Core for fast JSON serialisation/deserialization
  • Lombok for automatation of methods and logging

It also uses JUnit and Spring-test-starter (Only test scope)

QuickStart

Add the following VM Options or Environment Variables to your project:

-Dspring.profiles.active=dev
-DopenAI.api.key=YOUR_OPENAI_SECRET_KEY

In the case that you are using vscode, add a directory at root path named .vscode and inside a file named settings.json, paste the following so that vmargs work:

{
"java.test.config": {
        "vmArgs": ["-Dspring.profiles.active=dev", "-DopenAI.api.key=YOUR_API_KEY"]
    },
}

Add the following yaml file named openAIJava-application.yaml to configure this library:

spring:
  application:
    name: openai-client
  profiles: dev, stage
feign:
  client:
    config:
      default:
        connect-timeout: 160000000
        read-timeout: 160000000
openAIApi:
  url: https://api.openai.com/v1

---

spring:
  profiles: prod

feign:
  client:
    config:
      openaiapi:
        connect-timeout: 1000
        read-timeout: 2000

openaiapi:
  url: https://api.openai.com/v1

Use our Maven plugin that will provide the OpenAIClient object.

Maven

Java:

<dependency>
    <groupId>com.vaysai</groupId>
    <artifactId>openaijava</artifactId>
    <version>0.1.0</version>
</dependency>

Use the @EnableOpenAIAPI annotation in your main class

@EnableOpenAIApiClient
@SpringApplication
public class SomeApplication{
    
    public static void main(String[] args){
        SpringApplication.run(SomeApplication.class, args)
    }
}

Autowire an OpenAIClient object

@Autowired
OpenAIClient openAIClient;

Create a method calling an endpoint

void listModels(){
    ListModelsResponse response = openAIClient.listModels();
    log.info(response);
}

License

Apache 2.0

openaijava's People

Contributors

alexxriv avatar erickarroyo18 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

openaijava's Issues

Proxy Class

Complete proxy class to convert all messages and errors to same format so that if OpenAI returns an error code for any reason an error isn't thrown.

UTILS Library

Create library with constant libraries frequently used in OpenAI. For example name of available models, ("text-davinci-003", "text-babbage-002", etc). This could be made in constant static variables or ENUM objects.

Generic Objects for responses.

Maybe it would be a good idea to make all feign methods return the generic objects with different type arguments. For example: OpenAIResponse
OpenAIResponse

This needs to be discussed further to know advantages or disadvantages.

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.