Skip to content

Commit

Permalink
Merge pull request #1 from StackStorm-Exchange/transfer
Browse files Browse the repository at this point in the history
Transfer OpenAI pack from Incubator
  • Loading branch information
arm4b authored Sep 15, 2023
2 parents e2a3947 + dadb20a commit 2642578
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
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
1 change: 1 addition & 0 deletions .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ jobs:
uses: StackStorm-Exchange/ci/.github/workflows/pack-build_and_test.yaml@master
with:
enable-common-libs: false
python-version: 3.8
#apt-cache-version: v0
#py-cache-version: v0
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## v0.1.0

* Initial release
19 changes: 19 additions & 0 deletions README.md
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))
16 changes: 16 additions & 0 deletions actions/chat_completion.py
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}
]
)
16 changes: 16 additions & 0 deletions actions/chat_completion.yaml
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
11 changes: 11 additions & 0 deletions actions/image_generation.py
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)
16 changes: 16 additions & 0 deletions actions/image_generation.yaml
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
21 changes: 21 additions & 0 deletions aliases/chat_completion.yaml
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 %}
18 changes: 18 additions & 0 deletions aliases/image_generation.yaml
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 %}
12 changes: 12 additions & 0 deletions config.schema.yaml
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
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions openai.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openai_token: 'abcdefghikjl1234567890'
openai_org: 'nobody_org'
12 changes: 12 additions & 0 deletions pack.yaml
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openai>=0.27.0,<1.0
20 changes: 20 additions & 0 deletions tests/test_action_chat_completion.py
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"}]
)
18 changes: 18 additions & 0 deletions tests/test_action_image_generation.py
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")

0 comments on commit 2642578

Please sign in to comment.