Skip to content

Commit

Permalink
Merge pull request #39 from atomiechen/ft_linter
Browse files Browse the repository at this point in the history
add linter check
  • Loading branch information
atomiechen authored Jul 28, 2024
2 parents 6615411 + 747003f commit 837ebbc
Show file tree
Hide file tree
Showing 44 changed files with 1,871 additions and 1,270 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ temp*
.coverage*
.pytest_cache
htmlcov

# ruff output
.ruff_cache

7 changes: 4 additions & 3 deletions examples/legacy_OpenAIAPI/test_audio.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from handyllm import OpenAIAPI

from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
load_dotenv(find_dotenv())

file_path = '../assets/hello.m4a' # replace with your file path
file_path = "../assets/hello.m4a" # replace with your file path

with open(file_path, "rb") as file_bin:
response = OpenAIAPI.audio_transcriptions(
file=file_bin,
model='whisper-1',
model="whisper-1",
# timeout=10,
)
print(response['text'])
print(response["text"])
19 changes: 8 additions & 11 deletions examples/legacy_OpenAIAPI/test_completions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from handyllm import OpenAIAPI

from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand All @@ -14,10 +15,7 @@
def example_chat():
# ----- EXAMPLE 1 -----

prompt = [{
"role": "user",
"content": "please tell me a joke"
}]
prompt = [{"role": "user", "content": "please tell me a joke"}]
response = OpenAIAPI.chat(
model="gpt-3.5-turbo",
messages=prompt,
Expand All @@ -26,9 +24,9 @@ def example_chat():
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
timeout=10
)
print(response['choices'][0]['message']['content'])
timeout=10,
)
print(response["choices"][0]["message"]["content"])


print()
Expand All @@ -45,14 +43,13 @@ def example_completions():
max_tokens=256,
echo=True, # Echo back the prompt in addition to the completion
)
print(response['choices'][0]['text'])
print(response["choices"][0]["text"])


if __name__ == "__main__":
example_chat()

print()
print("-----")

example_completions()

example_completions()
1 change: 1 addition & 0 deletions examples/legacy_OpenAIAPI/test_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand Down
7 changes: 2 additions & 5 deletions examples/legacy_OpenAIAPI/test_files.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from handyllm import OpenAIAPI
from handyllm import utils

import json
from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand All @@ -14,8 +14,5 @@

## upload a file
with open("mydata.jsonl", "rb") as file_bin:
response = OpenAIAPI.files_upload(
file=file_bin,
purpose='fine-tune'
)
response = OpenAIAPI.files_upload(file=file_bin, purpose="fine-tune")
print(json.dumps(response, indent=2))
8 changes: 4 additions & 4 deletions examples/legacy_OpenAIAPI/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand All @@ -19,7 +20,7 @@
# timeout=10,
)
print(json.dumps(response, indent=2))
download_url = response['data'][0]['url']
download_url = response["data"][0]["url"]
file_path = utils.download_binary(download_url)
print(f"generated image: {file_path}")

Expand All @@ -36,7 +37,7 @@
# timeout=10,
)
print(json.dumps(response, indent=2))
download_url = response['data'][0]['url']
download_url = response["data"][0]["url"]
file_path = utils.download_binary(download_url)
print(f"edited image: {file_path}")

Expand All @@ -49,7 +50,6 @@
# timeout=10,
)
print(json.dumps(response, indent=2))
download_url = response['data'][0]['url']
download_url = response["data"][0]["url"]
file_path = utils.download_binary(download_url)
print(f"varied image: {file_path}")

20 changes: 8 additions & 12 deletions examples/legacy_OpenAIAPI/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from dotenv import load_dotenv, find_dotenv
from handyllm import OpenAIAPI

from dotenv import load_dotenv, find_dotenv
# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand All @@ -10,19 +11,15 @@
# OpenAIAPI.api_key = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# OpenAIAPI.organization = None

import logging
logging.basicConfig(level=logging.DEBUG)
my_logger = logging.getLogger(__file__)


def example_chat():
# ----- EXAMPLE 1 -----

prompt = [{
"role": "user",
"content": "please tell me a joke"
}]
response = OpenAIAPI.chat(
prompt = [{"role": "user", "content": "please tell me a joke"}]
OpenAIAPI.chat(
model="gpt-3.5-turbo",
messages=prompt,
temperature=0.2,
Expand All @@ -32,8 +29,8 @@ def example_chat():
presence_penalty=0.0,
timeout=10,
logger=my_logger,
log_marks=['mark: line 1', 'mark: line 2'],
)
log_marks=["mark: line 1", "mark: line 2"],
)


def example_completions():
Expand All @@ -47,9 +44,9 @@ def example_completions():
# echo=True, # Echo back the prompt in addition to the completion
stream=True,
logger=my_logger,
log_marks='you can also pass a string here',
log_marks="you can also pass a string here",
)
# If set stream to True, the response should be explicitly iterated to
# If set stream to True, the response should be explicitly iterated to
# make the logger work
for _ in OpenAIAPI.stream_completions(response):
pass
Expand All @@ -58,4 +55,3 @@ def example_completions():
if __name__ == "__main__":
example_chat()
example_completions()

4 changes: 3 additions & 1 deletion examples/legacy_OpenAIAPI/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand All @@ -11,13 +12,15 @@
# OpenAIAPI.api_key = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# OpenAIAPI.organization = None


def example_list():
## get all models
response = OpenAIAPI.models_list(
timeout=10,
)
print(json.dumps(response, indent=2))


def example_retrieve():
## retrieve a specific model
response = OpenAIAPI.models_retrieve(
Expand All @@ -30,4 +33,3 @@ def example_retrieve():
if __name__ == "__main__":
example_list()
example_retrieve()

1 change: 1 addition & 0 deletions examples/legacy_OpenAIAPI/test_moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand Down
21 changes: 9 additions & 12 deletions examples/legacy_OpenAIAPI/test_stream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from handyllm import OpenAIAPI

from dotenv import load_dotenv, find_dotenv

# load env parameters from file named .env
# API key is read from environment variable OPENAI_API_KEY
# organization is read from environment variable OPENAI_ORGANIZATION
Expand All @@ -14,10 +15,7 @@
def example_chat():
# ----- EXAMPLE 1 -----

prompt = [{
"role": "user",
"content": "please tell me a joke"
}]
prompt = [{"role": "user", "content": "please tell me a joke"}]
response = OpenAIAPI.chat(
model="gpt-3.5-turbo",
messages=prompt,
Expand All @@ -27,12 +25,12 @@ def example_chat():
frequency_penalty=0.0,
presence_penalty=0.0,
timeout=10,
stream=True
)
stream=True,
)

# you can use this to stream the response
for text in OpenAIAPI.stream_chat(response):
print(text, end='')
print(text, end="")

# or you can use this to get the whole response
# for chunk in response:
Expand All @@ -49,12 +47,12 @@ def example_completions():
timeout=10,
max_tokens=256,
echo=True, # Echo back the prompt in addition to the completion
stream=True
stream=True,
)

# you can use this to stream the response
for text in OpenAIAPI.stream_completions(response):
print(text, end='')
print(text, end="")

# or you can use this to get the whole response
# for chunk in response:
Expand All @@ -63,9 +61,8 @@ def example_completions():

if __name__ == "__main__":
example_chat()

print()
print("-----")

example_completions()

example_completions()
46 changes: 21 additions & 25 deletions examples/test_azure.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
from handyllm import OpenAIClient, utils
import os
import json

from handyllm import OpenAIClient, utils
from dotenv import load_dotenv, find_dotenv


# load env parameters from file
load_dotenv(find_dotenv())

import os
import json


def example_chat(client: OpenAIClient):
# ----- EXAMPLE 1 -----

prompt = [{
"role": "user",
"content": "please tell me a joke"
}]
prompt = [{"role": "user", "content": "please tell me a joke"}]
response = client.chat(
# this is the engine (i.e. deployment_id) parameter for Azure OpenAI API
engine="gpt-35-turbo",

# # OR: you can use model parameter and specify the model_engine_map
# # this is most useful for EndpointManager to unify API calls
# model="gpt-3.5-turbo",
# model_engine_map={"gpt-3.5-turbo": "gpt-35-turbo"},

messages=prompt,
temperature=0.2,
max_tokens=256,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
timeout=10,
).fetch()
print(response['choices'][0]['message']['content'])

).fetch()
print(response["choices"][0]["message"]["content"])


def example_embeddings(client: OpenAIClient):
Expand All @@ -51,33 +46,34 @@ def example_images_generations(client: OpenAIClient):
# ----- EXAMPLE 3 -----

response = client.images_generations(
api_version='2023-06-01-preview',
api_version="2023-06-01-preview",
prompt="A panda, synthwave style, digital painting",
n=1,
size="256x256",
).fetch()
print(json.dumps(response, indent=2))
download_url = response['data'][0]['url']
download_url = response["data"][0]["url"]
file_path = utils.download_binary(download_url)
print(f"generated image: {file_path}")


if __name__ == "__main__":
with OpenAIClient(
api_type='azure',
api_base=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION") # cannot be None if using Azure API.
) as client:
api_type="azure",
api_base=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_KEY"),
api_version=os.getenv(
"AZURE_OPENAI_API_VERSION"
), # cannot be None if using Azure API.
) as client:
example_chat(client)

print()
print("-----")

example_embeddings(client)

print()
print("-----")

example_images_generations(client)

example_images_generations(client)
Loading

0 comments on commit 837ebbc

Please sign in to comment.