Skip to content

Commit

Permalink
Adding chatops aliases, unit tests, misc corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
dalarsen committed Aug 15, 2023
1 parent 3646388 commit f73e74e
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 1 deletion.
16 changes: 16 additions & 0 deletions openai/.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 @floatingstatic

# 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
5 changes: 5 additions & 0 deletions openai/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## v0.1.0

* Initial release
4 changes: 4 additions & 0 deletions openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ The configuration schema contains two attributes:

* `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))
21 changes: 21 additions & 0 deletions openai/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 openai/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 %}
3 changes: 3 additions & 0 deletions openai/openai.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openai_token: 'abcdefghikjl1234567890'
openai_org: 'nobody_org'
2 changes: 1 addition & 1 deletion openai/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
openai
openai == 0.27.8
20 changes: 20 additions & 0 deletions openai/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 openai/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 f73e74e

Please sign in to comment.