Giter Site home page Giter Site logo

openai-java's Introduction

OpenAI Java

OpenAI GPT integration project using Java.

Features

This project provide the below features:

  • OpenAI Chat
  • OpenAI Assistants
  • Token Counter
    • Counts how many tokens exist in the input, estimate the output tokens, and then select a proper model to use it.
  • OpenAI Exception Handler
    • Consider specific exception scenarios to handle it.
  • SpringDoc OpenAPI UI (Swagger)
    • Library that helps to automate the generation of API documentation and provide an interface to test the solution.
  • Communication with Stream
    • Return the response in a asynchronous way.
  • Thread History
    • Assistant will consider or not the previous conversation in the future responses.
  • Knowledge Retrieval
    • "Retrieval augments the Assistant with knowledge from outside its model, such as proprietary product information or documents provided by your users. Once a file is uploaded and passed to the Assistant, OpenAI will automatically chunk your documents, index and store the embeddings, and implement vector search to retrieve relevant content to answer user queries."
    • Reference: https://platform.openai.com/docs/assistants/tools/knowledge-retrieval
  • Function Calling
    • "Similar to the Chat Completions API, the Assistants API supports function calling. Function calling allows you to describe functions to the Assistants and have it intelligently return the functions that need to be called along with their arguments. The Assistants API will pause execution during a Run when it invokes functions, and you can supply the results of the function call back to continue the Run execution."
    • Reference: https://platform.openai.com/docs/assistants/tools/function-calling

Installation

Download the project using the below GIT command:

$ git clone https://github.com/diogoaltoe/openai-java.git

Usage

Basically, you can use this project in 2 ways:

  • Via Swagger:
  • Via Test Cases:
    • Go to test folder to check the different ways to test and use the features.
    • NOTE: The test cases have a few examples on how to use the project, maybe it can help you.

Below we have more details on how to setup the OpenAI features.

OpenAI API

To use OpenAI API, you need to:

  • Go to OpenAI Platform Playground (https://platform.openai.com);
  • In the left menu, click on API keys option;
  • Click on + Create new secret key button;
  • Fill the Name field and click on Create secret key button;
  • Copy the key and set the OPENAI_API_KEY environment variable with it.
    • This environment variable will be used in the application.properties file inside src/main/resources folder:
      • openai-api-key=${OPENAI_API_KEY}

OpenAI Assistants

To use Assistants feature, you need to:

  • Go to OpenAI Platform Playground (https://platform.openai.com/playground);
  • Select Assistants option;
  • Create a new assistant;
  • In Name field, give a proper name for it;
  • In Instructions field, add these instructions:
  You are a chatbot for an ecommerce app.
  Try to be direct in you answer.
  If someone ask you to list something, use list numbers.
  • Copy the Assistant ID that is below to the Name field;
  • In the application.properties file inside src/main/resources folder, add:
    • openai.assistant.id={with-copied-assistant-id}

Knowledge Retrieval

To work with Retrieval feature, you need to:

  • Go to OpenAI Platform Playground (https://platform.openai.com/playground);
  • Select Assistants option;
  • In TOOLS option:
    • Enable Retrieval;
    • Upload the 2 files inside openai-java/src/main/resources:
      • info.md
      • policy.md

Functions Calling

To work with Functions feature, you need to:

  • Go to OpenAI Platform Playground (https://platform.openai.com/playground);
  • Select Assistants option;
  • In TOOLS option:
    • In Functions, click in +Function button;
    • Open the calculateShipping.json file inside openai-java/src/main/resources, copy the content and paste it in the function field;
    • Then, click on Save button;
    • It should save the new function and show below Functions the calculateShipping function name.

Libraries

License

This project is licensed under the MIT License.

openai-java's People

Contributors

diogoaltoe 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.