generated from StackStorm-Exchange/ci-pack-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from StackStorm-Exchange/transfer
Transfer OpenAI pack from Incubator
- Loading branch information
Showing
17 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# This is a comment. | ||
# Each line is a file pattern followed by one or more owners. | ||
|
||
# These owners will be the default owners for everything in | ||
# the repo. Unless a later match takes precedence, users and | ||
# teams which are put in owners will be requested for review | ||
# when someone opens a pull request. | ||
|
||
# This is base configuration. These owners could review the | ||
# whole file in this repository. | ||
* @dalarsen @StackStorm-Exchange/tsc | ||
|
||
# CI configuration files should be reviewed by specific owners | ||
# who are more responsible for ensuring the quality of this pack | ||
# or orchestrate StackStorm-Exchanges. | ||
.circleci/** @StackStorm-Exchange/tsc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## v0.1.0 | ||
|
||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# OpenAI Integration Pack | ||
|
||
Perform chat completions and image generation with OpenAI API | ||
|
||
## Configuration | ||
|
||
The configuration schema contains two attributes: | ||
|
||
* ``openai_token`` - Your OpenAI API access token | ||
* ``openai_org`` - The organization associated with your API token | ||
|
||
## Actions | ||
|
||
* `chat_completion` - Perform a chat completion query (text-based "chat" query) | ||
* `image_generation` - Request a OpenAI generative graphical image | ||
|
||
## Maintainers | ||
Active pack maintainers with review & write repository access: | ||
* Dave Larsen ([@dalarsen](https://github.com/dalarsen)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import openai | ||
|
||
from st2common.runners.base_action import Action | ||
|
||
|
||
class ChatCompletion(Action): | ||
def run(self, query, model): | ||
config = self.config | ||
openai.api_key = config.get("openai_token") | ||
openai.organization = config.get("openai_org") | ||
return openai.ChatCompletion.create( | ||
model=model, | ||
messages=[ | ||
{"role": "user", "content": query} | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: chat_completion | ||
pack: openai | ||
description: Action to query the OpenAI chat completion API. | ||
runner_type: "python-script" | ||
entry_point: chat_completion.py | ||
enabled: true | ||
parameters: | ||
query: | ||
required: true | ||
type: string | ||
secret: false | ||
model: | ||
required: false | ||
type: string | ||
default: "gpt-4" | ||
secret: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import openai | ||
|
||
from st2common.runners.base_action import Action | ||
|
||
|
||
class ImageGeneration(Action): | ||
def run(self, query, size): | ||
config = self.config | ||
openai.api_key = config.get("openai_token") | ||
openai.organization = config.get("openai_org") | ||
return openai.Image.create(prompt=query, size=size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: image_generation | ||
pack: openai | ||
description: Action to query the OpenAI image API. | ||
runner_type: "python-script" | ||
entry_point: image_generation.py | ||
enabled: true | ||
parameters: | ||
query: | ||
required: true | ||
type: string | ||
secret: false | ||
size: | ||
required: false | ||
type: string | ||
default: "512x512" | ||
secret: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
name: openai_chat_completion | ||
action_ref: openai.chat_completion | ||
description: Make a chat completion query to the OpenAI API | ||
formats: | ||
- "ask chatgpt {{query}}" | ||
ack: | ||
format: "Sending your ChatGPT chat completion query now, please wait..." | ||
enabled: true | ||
result: | ||
format: | | ||
Response from OpenAI for the ChatGPT chat completion query {{execution.parameters.question}}: | ||
{% if execution.status == 'succeeded' %} | ||
{% for choice in execution.result.result.choices %} | ||
----- | ||
{{ choice.message.content }} | ||
{% endfor %} | ||
{% else %} | ||
Query to OpenAI failed: {{execution}} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
name: openai_image_generation | ||
action_ref: openai.image_generation | ||
description: Make an image generation query to the OpenAI API | ||
formats: | ||
- "ask dall-e {{query}}" | ||
ack: | ||
format: "Sending your DALL-E image generation query now, please wait..." | ||
enabled: true | ||
result: | ||
format: | | ||
Response from OpenAI for the DALL-E image generation query {{execution.parameters.question}}: | ||
{% if execution.status == 'succeeded' %} | ||
{{ execution.result.result.data }} | ||
{% else %} | ||
Query to OpenAI failed: {{execution}} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
|
||
openai_token: | ||
description: "Authentication token for OpenAI API" | ||
type: string | ||
required: false | ||
secret: true | ||
openai_org: | ||
description: "Organization identifier for OpenAI API auth token" | ||
type: string | ||
required: false | ||
secret: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
openai_token: 'abcdefghikjl1234567890' | ||
openai_org: 'nobody_org' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
ref: openai | ||
name: OpenAI Query Pack | ||
description: Pack to query OpenAI API | ||
keywords: | ||
- openai | ||
- ai | ||
version: 0.1.0 | ||
python_versions: | ||
- "3" | ||
author: David Larsen | ||
email: dalarsen@expediagroup.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
openai>=0.27.0,<1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from st2tests.base import BaseActionTestCase | ||
from openai.openai_object import OpenAIObject | ||
|
||
import chat_completion | ||
import mock | ||
|
||
__all__ = ["TestChatCompletion"] | ||
|
||
|
||
class TestChatCompletion(BaseActionTestCase): | ||
action_cls = chat_completion.ChatCompletion | ||
|
||
@mock.patch("chat_completion.openai.ChatCompletion.create") | ||
def test_chat_completion_makes_valid_chat_completion_request(self, mock_openai): | ||
mock_openai.return_value = OpenAIObject() | ||
action = self.get_action_instance() | ||
action.run("fake query", "mock_model") | ||
mock_openai.assert_called_with( | ||
model="mock_model", messages=[{"role": "user", "content": "fake query"}] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from st2tests.base import BaseActionTestCase | ||
from openai.openai_object import OpenAIObject | ||
|
||
import image_generation | ||
import mock | ||
|
||
__all__ = ["TestChatCompletion"] | ||
|
||
|
||
class TestChatCompletion(BaseActionTestCase): | ||
action_cls = image_generation.ImageGeneration | ||
|
||
@mock.patch("chat_completion.openai.Image.create") | ||
def test_image_generation_makes_valid_image_generation_request(self, mock_openai): | ||
mock_openai.return_value = OpenAIObject() | ||
action = self.get_action_instance() | ||
action.run("fake query", "512x512") | ||
mock_openai.assert_called_with(prompt="fake query", size="512x512") |