Coder Social home page Coder Social logo

openai-connector's Introduction

OpenAI Connector for Mule 4.x

Overview

MuleSoft Connector for OpenAI's GPT APIs. It leverages the Java libraries from this project from Theo Kanning. An example of using the connector can be found here

Installation Instructions

  1. Clone the repository
  2. Deploy the connector to your local Maven repository using the mvn clean install command. If you are using Maven 3.8.1+, you may run into issues with HTTP repositories being blocked. Follow the help article here to resolve the issue.
  3. Add the connector dependency to your project pom.xml file
<dependency>
    <groupId>com.mulesoft.platform_se</groupId>
    <artifactId>openai</artifactId>
    <version>1.0.7</version>
    <classifier>mule-plugin</classifier>
</dependency>

Configuration

  1. Log into your OpenAI account
  2. In your account settings, click View API keys.
  3. Click + Create new secret key
  4. Copy your API key.

Operations

Create Chat Completion

This operation will send a chat conversation to OpenAPI and generate a chat completion. Documentation for the OpenAI API can be found here

Code Example

<flow name="chatCompletionFlow">
	<http:listener config-ref="HTTP_Listener_config" path="/chat" />
	<openai:create-chat-completion config-ref="OpenAI_Config" model="gpt-3.5-turbo">
		<openai:messages><![CDATA[#[output application/java
			---
			[{
				role: "user",
				content: "Hello!"
			}]]]]>
		</openai:messages>
		<openai:logit-biases><![CDATA[#[output application/java
			---
			[
				{
					tokenId: "30",
					biasValue: -100
				}
			]]]]>
		</openai:logit-biases>
	</openai:create-chat-completion>
	<ee:transform>
		<ee:message>
			<ee:set-payload><![CDATA[%dw 2.0
				output application/json
				---
				payload]]>
			</ee:set-payload>
		</ee:message>
	</ee:transform>
</flow>

Create Completion

This operation will send a prompt to OpenAI and generate a completion. Documentation for the OpenAI API can be found here

Code Example

<flow name="completionFlow">
	<http:listener config-ref="HTTP_Listener_config" path="/completion" />
	<openai:create-completion config-ref="OpenAI_Config" model="text-davinci-003" maxTokens="7" temperature="0">
		<openai:prompt><![CDATA[Say this is a test]]></openai:prompt>
		<openai:logit-biases><![CDATA[#[output application/java
			---
			[{
				tokenId: "1332",
				biasValue: -100
			},{
				tokenId: "257",
				biasValue: -100
			}]]]]>
		</openai:logit-biases>
	</openai:create-completion>
	<ee:transform>
		<ee:message>
			<ee:set-payload><![CDATA[%dw 2.0
				output application/json
				---
				payload]]>
			</ee:set-payload>
		</ee:message>
	</ee:transform>
</flow>

Create Embeddings

This operation will send input text(s) to OpenAI and generate vector representations that can be easily consumed by machine learning models and algorithms. Documentation for the OpenAI API can be found here

Code Example

<flow name="embeddingsFlow">
	<http:listener config-ref="HTTP_Listener_config" path="/embeddings" />
	<openai:create-embeddings config-ref="OpenAI_Config" model="text-embedding-ada-002">
		<openai:inputs><![CDATA[#[output application/java
			---
			[
				"The food was delicious and the waiter...",
				"How are you?"
			]]]]>
		</openai:inputs>
	</openai:create-embeddings>
	<ee:transform>
		<ee:message>
			<ee:set-payload><![CDATA[%dw 2.0
				output application/json
				---
				payload]]>
			</ee:set-payload>
		</ee:message>
	</ee:transform>
</flow>

Create Image File

This operation will generate an image file(s) with DALL-E given a prompt. response_format is set to b64_json for this operation and the output is a Base64 string. Documentation for the OpenAI API can be found here

Code Example

<flow name="imageFileFlow">
	<http:listener config-ref="HTTP_Listener_config" path="/image"/>
	<openai:create-image-file config-ref="OpenAI_Config" n="2" size="256x256">
		<openai:prompt ><![CDATA[Penguin]]></openai:prompt>
	</openai:create-image-file>
	<foreach>
		<ee:transform>
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
					import * from dw::core::Binaries
					output application/octet-stream
					---
					fromBase64(payload)
					]]>
				</ee:set-payload>
			</ee:message>
		</ee:transform>
		<file:write path='#["C:\image-" ++ vars.counter ++ ".png"]' />
	</foreach>
</flow>

Generate Image URL

This operation will generate an image URL(s) with DALL-E given a prompt. response_format is set to url for this operation and the output is a URL. Documentation for the OpenAI API can be found here

Code Example

<flow name="imageUrlFlow">
	<http:listener config-ref="HTTP_Listener_config" path="/url"/>
	<openai:create-image-url config-ref="OpenAI_Config" size="256x256">
		<openai:prompt ><![CDATA[elephant wearing red shoes]]></openai:prompt>
	</openai:create-image-url>
	<http:request method="GET" url="#[payload[0]]"/>
</flow>

About

Author: Dejim Juang - [email protected], Sapandeep Matharoo - [email protected]
Last Update: March 15, 2023

openai-connector's People

Contributors

djuang1 avatar sikhness 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.