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
- Clone the repository
- 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. - 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>
- Log into your OpenAI account
- In your account settings, click View API keys.
- Click
+ Create new secret key
- Copy your API key.
This operation will send a chat conversation to OpenAPI and generate a chat completion. Documentation for the OpenAI API can be found here
<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>
This operation will send a prompt to OpenAI and generate a completion. Documentation for the OpenAI API can be found here
<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>
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
<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>
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
<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>
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
<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>
Author: Dejim Juang - [email protected], Sapandeep Matharoo - [email protected]
Last Update: March 15, 2023