Edit on GitHub

communex.module.example.gpt

 1import json
 2from enum import Enum
 3from os import getenv
 4
 5from fastapi import HTTPException
 6from openai import OpenAI  # type: ignore
 7
 8from communex.module.module import Module, endpoint
 9from communex.module.server import ModuleServer
10
11OPENAI_API_KEY = getenv("OPENAI_API_KEY")
12
13
14class OpenAIModels(str, Enum):
15    three = "gpt-3.5-turbo"
16
17
18class OpenAIModule(Module):
19    def __init__(self) -> None:
20        super().__init__()
21        self.client = OpenAI(api_key=OPENAI_API_KEY)  #  type: ignore
22
23    @endpoint
24    def prompt(self, text: str, model: OpenAIModels):
25        response = self.client.chat.completions.create(  # type: ignore
26            model=model,
27            response_format={"type": "json_object"},
28            messages=[
29                {"role": "system",
30                    "content": "You are a helpful assistant designed to output JSON."},
31                {"role": "user", "content": text},
32            ],
33        )
34        answers: list[dict[str, str]] = []
35        for msg in response.choices:  # type: ignore
36            finish_reason = msg.finish_reason  # type: ignore
37            if finish_reason != "stop":
38                raise HTTPException(418, finish_reason)
39            content = msg.message.content  # type: ignore
40            if content:
41                answers.append(json.loads(content))  # type: ignore
42
43        return {"Answer": answers}
44
45
46if __name__ == "__main__":
47    import uvicorn
48
49    from communex.compat.key import classic_load_key
50
51    model = OpenAIModule()
52    key = classic_load_key("test")
53    model_server = ModuleServer(model, key)
54    app = model_server.get_fastapi_app()
55
56    uvicorn.run(app, host="127.0.0.1", port=8000)
OPENAI_API_KEY = None
class OpenAIModels(builtins.str, enum.Enum):
15class OpenAIModels(str, Enum):
16    three = "gpt-3.5-turbo"

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

three = <OpenAIModels.three: 'gpt-3.5-turbo'>
Inherited Members
enum.Enum
name
value
builtins.str
encode
replace
split
rsplit
join
capitalize
casefold
title
center
count
expandtabs
find
partition
index
ljust
lower
lstrip
rfind
rindex
rjust
rstrip
rpartition
splitlines
strip
swapcase
translate
upper
startswith
endswith
removeprefix
removesuffix
isascii
islower
isupper
istitle
isspace
isdecimal
isdigit
isnumeric
isalpha
isalnum
isidentifier
isprintable
zfill
format
format_map
maketrans
class OpenAIModule(communex.module.module.Module):
19class OpenAIModule(Module):
20    def __init__(self) -> None:
21        super().__init__()
22        self.client = OpenAI(api_key=OPENAI_API_KEY)  #  type: ignore
23
24    @endpoint
25    def prompt(self, text: str, model: OpenAIModels):
26        response = self.client.chat.completions.create(  # type: ignore
27            model=model,
28            response_format={"type": "json_object"},
29            messages=[
30                {"role": "system",
31                    "content": "You are a helpful assistant designed to output JSON."},
32                {"role": "user", "content": text},
33            ],
34        )
35        answers: list[dict[str, str]] = []
36        for msg in response.choices:  # type: ignore
37            finish_reason = msg.finish_reason  # type: ignore
38            if finish_reason != "stop":
39                raise HTTPException(418, finish_reason)
40            content = msg.message.content  # type: ignore
41            if content:
42                answers.append(json.loads(content))  # type: ignore
43
44        return {"Answer": answers}
client
@endpoint
def prompt(self, text: str, model: OpenAIModels):
24    @endpoint
25    def prompt(self, text: str, model: OpenAIModels):
26        response = self.client.chat.completions.create(  # type: ignore
27            model=model,
28            response_format={"type": "json_object"},
29            messages=[
30                {"role": "system",
31                    "content": "You are a helpful assistant designed to output JSON."},
32                {"role": "user", "content": text},
33            ],
34        )
35        answers: list[dict[str, str]] = []
36        for msg in response.choices:  # type: ignore
37            finish_reason = msg.finish_reason  # type: ignore
38            if finish_reason != "stop":
39                raise HTTPException(418, finish_reason)
40            content = msg.message.content  # type: ignore
41            if content:
42                answers.append(json.loads(content))  # type: ignore
43
44        return {"Answer": answers}