diff --git a/README.md b/README.md index b662c25..fe3f85e 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,84 @@ ## Overview -[//]: # (TODO: Add overview mentioning the purpose of the module, supported REST API versions, and other high-level details.) +[OpenAI](https://openai.com/), an AI research organization dedicated to developing beneficial AI for humanity, provides the [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) for accessing its powerful AI models, enabling tasks such as natural language processing and image generation. + +The `ballarinax/openai.chat` package offers functionality to connect and interact with [chat completion related endpoints of OpenAI REST API v1](https://platform.openai.com/docs/api-reference/chat) Enabling seamless interaction with the advanced GPT-4 models developed by OpenAI for diverse conversational and text generation tasks. ## Setup guide -[//]: # (TODO: Add detailed steps to obtain credentials and configure the module.) +To use the OpenAI Connector, you must have access to the OpenAI API through a [OpenAI Platform account](https://platform.openai.com) and a project under it. If you do not have a OpenAI Platform account, you can sign up for one [here](https://platform.openai.com/signup). + +#### Create a OpenAI API Key + +1. Open the [OpenAI Platform Dashboard](https://platform.openai.com). + +2. Navigate to **Dashboard** -> **API keys**. +Navigate to API key section + +3. Click on the **Create new secret key** button. +OpenAI Dashbaord | API keys + +4. Fill the details and click on Create secret key. +Create New API key + +5. Store the API key securely to use in your application. +Generated New API key ## Quickstart -[//]: # (TODO: Add a quickstart guide to demonstrate a basic functionality of the module, including sample code snippets.) +To use the `OpenAI Chat` connector in your Ballerina application, update the `.bal` file as follows: + +### Step 1: Import the module + +Import the `ballerinax/openai.chat` module. + +```ballerina +import ballerinax/openai.chat; +``` + +### Step 2: Create a new connector instance + +Create a `chat:Client` with the obtained API Key and initialize the connector. + +```ballerina +configurable string token = ?; + +final chat:Client openAIChat = check new({ + auth: { + token + } +}); +``` + +### Step 3: Invoke the connector operation + +Now, you can utilize available connector operations. + +#### Generate a response for given message + +```ballerina + +public function main() returns error? { + + // Create a chat completion request. + CreateChatCompletionRequest request = { + model: "gpt-4o-mini", + messages: [{ + "role": "user", + "content": "What is Ballerina programming language?" + }] + }; + + CreateChatCompletionResponse response = check openAIChat->/chat/completions.post(request); +} +``` + +### Step 4: Run the Ballerina application + +```bash +bal run +``` ## Examples diff --git a/ballerina/Module.md b/ballerina/Module.md index fdd9db8..0dd4929 100644 --- a/ballerina/Module.md +++ b/ballerina/Module.md @@ -1,14 +1,82 @@ ## Overview -[//]: # (TODO: Add overview mentioning the purpose of the module, supported REST API versions, and other high-level details.) +[OpenAI](https://openai.com/), an AI research organization dedicated to developing beneficial AI for humanity, provides the [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) for accessing its powerful AI models, enabling tasks such as natural language processing and image generation. + +The `ballarinax/openai.chat` package offers functionality to connect and interact with [chat completion related endpoints of OpenAI REST API v1](https://platform.openai.com/docs/api-reference/chat) Enabling seamless interaction with the advanced GPT-4 models developed by OpenAI for diverse conversational and text generation tasks. ## Setup guide -[//]: # (TODO: Add detailed steps to obtain credentials and configure the module.) +To use the OpenAI Connector, you must have access to the OpenAI API through a [OpenAI Platform account](https://platform.openai.com) and a project under it. If you do not have a OpenAI Platform account, you can sign up for one [here](https://platform.openai.com/signup). + +#### Create a OpenAI API Key + +1. Open the [OpenAI Platform Dashboard](https://platform.openai.com). + +2. Navigate to **Dashboard** -> **API keys**. +Navigate to API key section + +3. Click on the **Create new secret key** button. +OpenAI Dashbaord | API keys + +4. Fill the details and click on Create secret key. +Create New API key + +5. Store the API key securely to use in your application. +Generated New API key ## Quickstart -[//]: # (TODO: Add a quickstart guide to demonstrate a basic functionality of the module, including sample code snippets.) +To use the `OpenAI Chat` connector in your Ballerina application, update the `.bal` file as follows: + +### Step 1: Import the module + +Import the `ballerinax/openai.chat` module. + +```ballerina +import ballerinax/openai.chat; +``` + +### Step 2: Create a new connector instance + +Create a `chat:Client` with the obtained API Key and initialize the connector. + +```ballerina +configurable string token = ?; + +final chat:Client openAIChat = check new({ + auth: { + token + } +}); +``` + +### Step 3: Invoke the connector operation + +Now, you can utilize available connector operations. + +#### Generate a response for given message + +```ballerina +public function main() returns error? { + + // Create a chat completion request. + CreateChatCompletionRequest request = { + model: "gpt-4o-mini", + messages: [{ + "role": "user", + "content": "What is Ballerina programming language?" + }] + }; + + CreateChatCompletionResponse response = check openAIChat->/chat/completions.post(request); +} +``` + +### Step 4: Run the Ballerina application + +```bash +bal run +``` ## Examples diff --git a/ballerina/Package.md b/ballerina/Package.md index fdd9db8..0dd4929 100644 --- a/ballerina/Package.md +++ b/ballerina/Package.md @@ -1,14 +1,82 @@ ## Overview -[//]: # (TODO: Add overview mentioning the purpose of the module, supported REST API versions, and other high-level details.) +[OpenAI](https://openai.com/), an AI research organization dedicated to developing beneficial AI for humanity, provides the [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) for accessing its powerful AI models, enabling tasks such as natural language processing and image generation. + +The `ballarinax/openai.chat` package offers functionality to connect and interact with [chat completion related endpoints of OpenAI REST API v1](https://platform.openai.com/docs/api-reference/chat) Enabling seamless interaction with the advanced GPT-4 models developed by OpenAI for diverse conversational and text generation tasks. ## Setup guide -[//]: # (TODO: Add detailed steps to obtain credentials and configure the module.) +To use the OpenAI Connector, you must have access to the OpenAI API through a [OpenAI Platform account](https://platform.openai.com) and a project under it. If you do not have a OpenAI Platform account, you can sign up for one [here](https://platform.openai.com/signup). + +#### Create a OpenAI API Key + +1. Open the [OpenAI Platform Dashboard](https://platform.openai.com). + +2. Navigate to **Dashboard** -> **API keys**. +Navigate to API key section + +3. Click on the **Create new secret key** button. +OpenAI Dashbaord | API keys + +4. Fill the details and click on Create secret key. +Create New API key + +5. Store the API key securely to use in your application. +Generated New API key ## Quickstart -[//]: # (TODO: Add a quickstart guide to demonstrate a basic functionality of the module, including sample code snippets.) +To use the `OpenAI Chat` connector in your Ballerina application, update the `.bal` file as follows: + +### Step 1: Import the module + +Import the `ballerinax/openai.chat` module. + +```ballerina +import ballerinax/openai.chat; +``` + +### Step 2: Create a new connector instance + +Create a `chat:Client` with the obtained API Key and initialize the connector. + +```ballerina +configurable string token = ?; + +final chat:Client openAIChat = check new({ + auth: { + token + } +}); +``` + +### Step 3: Invoke the connector operation + +Now, you can utilize available connector operations. + +#### Generate a response for given message + +```ballerina +public function main() returns error? { + + // Create a chat completion request. + CreateChatCompletionRequest request = { + model: "gpt-4o-mini", + messages: [{ + "role": "user", + "content": "What is Ballerina programming language?" + }] + }; + + CreateChatCompletionResponse response = check openAIChat->/chat/completions.post(request); +} +``` + +### Step 4: Run the Ballerina application + +```bash +bal run +``` ## Examples diff --git a/docs/setup/resources/api-key-dashboard.png b/docs/setup/resources/api-key-dashboard.png new file mode 100644 index 0000000..0a66883 Binary files /dev/null and b/docs/setup/resources/api-key-dashboard.png differ diff --git a/docs/setup/resources/create-new-secrete-key.png b/docs/setup/resources/create-new-secrete-key.png new file mode 100644 index 0000000..5492f4a Binary files /dev/null and b/docs/setup/resources/create-new-secrete-key.png differ diff --git a/docs/setup/resources/navigate-api-key-dashboard.png b/docs/setup/resources/navigate-api-key-dashboard.png new file mode 100644 index 0000000..f42cb48 Binary files /dev/null and b/docs/setup/resources/navigate-api-key-dashboard.png differ diff --git a/docs/setup/resources/saved-key.png b/docs/setup/resources/saved-key.png new file mode 100644 index 0000000..7ed28c7 Binary files /dev/null and b/docs/setup/resources/saved-key.png differ