-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds plugin for OpenAI's Generative Artificial Intelligence platform (#…
…3159) * Adds plugin to setup.py * Adds plugin code * Improves config * ruff ruff ruff * Switches to OpenAI's official Python library * ruff ruff ruff * Returns a dict representation of the OpenAIObject
- Loading branch information
Showing
7 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
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
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,15 @@ | ||
""" | ||
.. module: dispatch.plugins.bases.artificial_intelligence | ||
:platform: Unix | ||
:copyright: (c) 2019 by Netflix Inc., see AUTHORS for more | ||
:license: Apache, see LICENSE for more details. | ||
.. moduleauthor:: Marc Vilanova <[email protected]> | ||
""" | ||
from dispatch.plugins.base import Plugin | ||
|
||
|
||
class ArtificialIntelligencePlugin(Plugin): | ||
type = "artificial-intelligence" | ||
|
||
def ask(self, items, **kwargs): | ||
raise NotImplementedError |
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 @@ | ||
from ._version import __version__ # noqa |
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 @@ | ||
__version__ = "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,31 @@ | ||
from pydantic import Field, SecretStr | ||
from typing import List, TypeVar | ||
|
||
from dispatch.config import BaseConfigurationModel | ||
|
||
Stop = TypeVar("Stop", str, List) | ||
|
||
|
||
class OpenAIConfiguration(BaseConfigurationModel): | ||
"""OpenAI configuration description.""" | ||
|
||
api_key: SecretStr = Field(title="API Key", description="Your secret OpenAI API key.") | ||
model: str = Field("text-davinci-003", title="Model", description="") | ||
max_tokens: int = Field( | ||
50, | ||
title="Max Tokens", | ||
description="The maximum number of tokens to generate in the completion.", | ||
) | ||
temperature: float = Field( | ||
1, title="Temperature", description="What sampling temperature to use, between 0 and 2." | ||
) | ||
n: int = Field( | ||
1, | ||
title="Number of completions (n)", | ||
description="How many completions to generate for each prompt.", | ||
) | ||
stop: Stop = Field( | ||
None, | ||
title="Stop", | ||
description="Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.", | ||
) |
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,53 @@ | ||
""" | ||
.. module: dispatch.plugins.openai.plugin | ||
:platform: Unix | ||
:copyright: (c) 2019 by Netflix Inc., see AUTHORS for more | ||
:license: Apache, see LICENSE for more details. | ||
.. moduleauthor:: Marc Vilanova <[email protected]> | ||
""" | ||
import logging | ||
|
||
import openai | ||
from openai import util | ||
|
||
from dispatch.decorators import apply, counter, timer | ||
from dispatch.plugins import dispatch_openai as openai_plugin | ||
from dispatch.plugins.bases import ArtificialIntelligencePlugin | ||
from dispatch.plugins.dispatch_openai.config import ( | ||
OpenAIConfiguration, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@apply(counter, exclude=["__init__"]) | ||
@apply(timer, exclude=["__init__"]) | ||
class OpenAIPlugin(ArtificialIntelligencePlugin): | ||
title = "OpenAI Plugin - Generative Artificial Intelligence" | ||
slug = "openai-artificial-intelligence" | ||
description = "Uses OpenAI's platform to allow users to ask questions in natural language." | ||
version = openai_plugin.__version__ | ||
|
||
author = "Netflix" | ||
author_url = "https://github.com/netflix/dispatch.git" | ||
|
||
def __init__(self): | ||
self.configuration_schema = OpenAIConfiguration | ||
|
||
def ask(self, prompt: str) -> dict: | ||
openai.api_key = self.api_key | ||
|
||
try: | ||
response = openai.Completion.create( | ||
max_tokens=self.max_tokens, | ||
model=self.model, | ||
n=self.n, | ||
prompt=prompt, | ||
stop=self.stop, | ||
temperature=self.temperature, | ||
) | ||
except Exception as e: | ||
logger.error(e) | ||
raise | ||
|
||
return util.convert_to_dict(response) |