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

Fallback to tokens API if local tokenizer fails #457

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Changes from 1 commit
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
56 changes: 37 additions & 19 deletions src/cohere/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,16 @@ def tokenize(
) -> TokenizeResponse:
# `offline` parameter controls whether to use an offline tokenizer. If set to True, the tokenizer config will be downloaded (and cached),
# and the request will be processed using the offline tokenizer. If set to False, the request will be processed using the API. The default value is True.
opts: RequestOptions = request_options or {} # type: ignore
if offline:
tokens = asyncio.run(local_tokenizers.local_tokenize(self, text=text, model=model))
return TokenizeResponse(tokens=tokens, token_strings=[])
else:
return super().tokenize(text=text, model=model, request_options=request_options)
try:
tokens = asyncio.run(local_tokenizers.local_tokenize(self, text=text, model=model))
return TokenizeResponse(tokens=tokens, token_strings=[])
except Exception:
opts["additional_headers"] = opts.get("additional_headers", {})
opts["additional_headers"]["sdk-api-warning-message"] = "offline_tokenizer_failed"
print(opts)
abdullahkady marked this conversation as resolved.
Show resolved Hide resolved
return super().tokenize(text=text, model=model, request_options=opts)

def detokenize(
self,
Expand All @@ -223,12 +228,17 @@ def detokenize(
) -> DetokenizeResponse:
# `offline` parameter controls whether to use an offline tokenizer. If set to True, the tokenizer config will be downloaded (and cached),
# and the request will be processed using the offline tokenizer. If set to False, the request will be processed using the API. The default value is True.
opts: RequestOptions = request_options or {} # type: ignore
if offline:
model = model or "command"
text = asyncio.run(local_tokenizers.local_detokenize(self, model=model, tokens=tokens))
return DetokenizeResponse(text=text)
else:
return super().detokenize(tokens=tokens, model=model, request_options=request_options)
try:
model = model or "command"
abdullahkady marked this conversation as resolved.
Show resolved Hide resolved
text = asyncio.run(local_tokenizers.local_detokenize(self, model=model, tokens=tokens))
return DetokenizeResponse(text=text)
except Exception:
opts["additional_headers"] = opts.get("additional_headers", {})
opts["additional_headers"]["sdk-api-warning-message"] = "offline_tokenizer_failed"

return super().detokenize(tokens=tokens, model=model, request_options=opts)

def fetch_tokenizer(self, *, model: str) -> Tokenizer:
"""
Expand Down Expand Up @@ -376,12 +386,16 @@ async def tokenize(
) -> TokenizeResponse:
# `offline` parameter controls whether to use an offline tokenizer. If set to True, the tokenizer config will be downloaded (and cached),
# and the request will be processed using the offline tokenizer. If set to False, the request will be processed using the API. The default value is True.
opts: RequestOptions = request_options or {} # type: ignore
if offline:
model = model or "command"
tokens = await local_tokenizers.local_tokenize(self, model=model, text=text)
return TokenizeResponse(tokens=tokens, token_strings=[])
else:
return await super().tokenize(text=text, model=model, request_options=request_options)
try:
tokens = await local_tokenizers.local_tokenize(self, model=model, text=text)
return TokenizeResponse(tokens=tokens, token_strings=[])
except Exception:
opts["additional_headers"] = opts.get("additional_headers", {})
opts["additional_headers"]["sdk-api-warning-message"] = "offline_tokenizer_failed"

return await super().tokenize(text=text, model=model, request_options=opts)

async def detokenize(
self,
Expand All @@ -393,12 +407,16 @@ async def detokenize(
) -> DetokenizeResponse:
# `offline` parameter controls whether to use an offline tokenizer. If set to True, the tokenizer config will be downloaded (and cached),
# and the request will be processed using the offline tokenizer. If set to False, the request will be processed using the API. The default value is True.
opts: RequestOptions = request_options or {} # type: ignore
if offline:
model = model or "command"
text = await local_tokenizers.local_detokenize(self, model=model, tokens=tokens)
return DetokenizeResponse(text=text)
else:
return await super().detokenize(tokens=tokens, model=model, request_options=request_options)
try:
text = await local_tokenizers.local_detokenize(self, model=model, tokens=tokens)
return DetokenizeResponse(text=text)
except Exception:
opts["additional_headers"] = opts.get("additional_headers", {})
opts["additional_headers"]["sdk-api-warning-message"] = "offline_tokenizer_failed"

return await super().detokenize(tokens=tokens, model=model, request_options=opts)

async def fetch_tokenizer(self, *, model: str) -> Tokenizer:
"""
Expand Down
Loading