Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fallback tokenizer #1046

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions outlines/models/openai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration with OpenAI's API."""
import warnings
import functools
from dataclasses import asdict, dataclass, field, replace
from itertools import zip_longest
Expand Down Expand Up @@ -439,7 +440,14 @@ def openai_model(
config = OpenAIConfig(model=model_name)

client = AsyncOpenAI(**openai_client_params)
tokenizer = tiktoken.encoding_for_model(model_name)

try:
tokenizer = tiktoken.encoding_for_model(model_name)
except KeyError:
JerryKwan marked this conversation as resolved.
Show resolved Hide resolved
warnings.warn(
f"Could not find a tokenizer for model {model_name}. Using default cl100k_base."
)
tokenizer = tiktoken.get_encoding("cl100k_base")

return OpenAI(client, config, tokenizer)

Expand All @@ -464,6 +472,13 @@ def azure_openai(
config = OpenAIConfig(model=deployment_name)

client = AsyncAzureOpenAI(**azure_openai_client_params)
tokenizer = tiktoken.encoding_for_model(model_name or deployment_name)

try:
tokenizer = tiktoken.encoding_for_model(model_name or deployment_name)
except KeyError:
warnings.warn(
f"Could not find a tokenizer for model {model_name or deployment_name}. Using default cl100k_base."
)
tokenizer = tiktoken.get_encoding("cl100k_base")

return OpenAI(client, config, tokenizer)
Loading