Skip to content

Commit

Permalink
added text moderation, dimensions to embeddings and new models
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastropy committed Jan 29, 2024
1 parent 5b95928 commit e1ef919
Show file tree
Hide file tree
Showing 4 changed files with 926 additions and 3 deletions.
24 changes: 24 additions & 0 deletions serverless_openai/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class OpenAIAPI(BaseModel):
completion_url: HttpUrlString = "https://api.openai.com/v1/chat/completions"
imagecreation_url: HttpUrlString = "https://api.openai.com/v1/images/generations"
embeddings_url: HttpUrlString = "https://api.openai.com/v1/embeddings"
moderation_url: HttpUrlString = "https://api.openai.com/v1/moderations"


@validator('headers', always=True)
Expand Down Expand Up @@ -280,12 +281,14 @@ def embeddings(
self,
prompt: EmbeddingPrompts,
model: EmbeddingModels = EmbeddingModels.ada2,
dimensions: int = 1536,
tries: int = 5,
timeout: int = 500,
) -> OpenAIResults:
data = {
"model": model,
"input": prompt,
"dimensions": dimensions
}

res = {}
Expand All @@ -298,6 +301,27 @@ def embeddings(
print("ERROR:", e)
return OpenAIResults(result=False, result_json=res)

def moderation(
self,
prompt: str,
model: ModerationModels = ModerationModels.moderation_latest,
tries: int = 5,
timeout: int = 500,
) -> OpenAIResults:
data = {
"model": model,
"input": prompt,
}
res = {}
for _ in range(tries):
try:
res = requests.post(self.moderation_url, headers=self.headers, json=data, timeout=timeout).json()
if 'results' in res:
return OpenAIResults(result=res['results'][0]['flagged'], result_json=res)
except Exception as e:
print("ERROR:", e)
return OpenAIResults(result=False, result_json=res)

class ScrapingBeeAPI(BaseModel):
api_key: str
scrape_url: HttpUrlString = "https://app.scrapingbee.com/api/v1"
Expand Down
7 changes: 7 additions & 0 deletions serverless_openai/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Roles(str, ExtendedEnum):
class TextCompletionModels(str, ExtendedEnum):
gpt4_1106 : str = "gpt-4-1106-preview"
gpt4 : str = "gpt-4"
gpt4_turbo : str = "gpt-4-turbo-preview"
gpt4_0125 : str = "gpt-4-0125-preview"
gpt35_turbo_0125: str = "gpt-3.5-turbo-0125"
gpt35_turbo_1106 : str = "gpt-3.5-turbo-1106"
gpt35_turbo_16k : str = "gpt-3.5-turbo-16k"
gpt35_turbo : str = "gpt-3.5-turbo"
Expand Down Expand Up @@ -66,6 +69,10 @@ class EmbeddingModels(str, ExtendedEnum):
class EmbeddingPrompts(BaseModel):
prompt: Union[str, List[str]]

class ModerationModels(str, ExtendedEnum):
moderation_stable : str = "text-moderation-stable"
moderation_latest : str = "text-moderation-latest"

class Similarity(BaseModel):
vector: List[List[float]]
matrix: List[List[float]]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '1.2.8'
VERSION = '1.2.9'
DESCRIPTION = "A package for using Openai in serverless environment"
LONG_DESCRIPTION = 'A package for using Openai with scraping and etc. in serverless application such as AWS Lambda and GCP Cloud Function'

Expand Down
Loading

0 comments on commit e1ef919

Please sign in to comment.