-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement loglikelihood endpoint (#277)
* implement loglikelihood endpoint * poetry update
- Loading branch information
1 parent
f60bd70
commit ee55ca9
Showing
10 changed files
with
280 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Dict, List, NamedTuple, Optional | ||
|
||
TokenLogLikelihood = NamedTuple("TokenLogLikelihood", [("encoded", int), ("decoded", str), ("log_likelihood", float)]) | ||
|
||
|
||
class LogLikelihoods: | ||
@staticmethod | ||
def token_list_from_dict(token_list: Optional[List[Dict]]): | ||
if token_list is None: | ||
return None | ||
return [TokenLogLikelihood(**token) for token in token_list] | ||
|
||
def __init__(self, prompt_tokens: List[TokenLogLikelihood], completion_tokens: List[TokenLogLikelihood]): | ||
self.prompt_tokens = self.token_list_from_dict(prompt_tokens) | ||
self.completion_tokens = self.token_list_from_dict(completion_tokens) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
|
||
from cohere.responses import LogLikelihoods | ||
|
||
TEST_MODEL = "command-light" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_basic_llh_async(async_client): | ||
resp = await async_client.loglikelihood(model=TEST_MODEL, prompt="co:here", completion="co:where?") | ||
assert isinstance(resp, LogLikelihoods) | ||
assert isinstance(resp.prompt_tokens[0].encoded, int) | ||
assert isinstance(resp.prompt_tokens[0].decoded, str) | ||
assert isinstance(resp.prompt_tokens[1].log_likelihood, float) | ||
|
||
assert resp.prompt_tokens[0].decoded == "<BOS_TOKEN>" | ||
assert resp.prompt_tokens[-1].decoded == "<EOP_TOKEN>" | ||
|
||
assert isinstance(resp.completion_tokens[0].encoded, int) | ||
assert isinstance(resp.completion_tokens[0].decoded, str) | ||
assert isinstance(resp.completion_tokens[0].log_likelihood, float) | ||
|
||
assert resp.completion_tokens[-1].decoded == "<EOS_TOKEN>" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_only_prompt_async_llh(async_client): | ||
resp = await async_client.loglikelihood(model=TEST_MODEL, prompt="co:here") | ||
assert isinstance(resp, LogLikelihoods) | ||
assert isinstance(resp.prompt_tokens[0].encoded, int) | ||
assert resp.completion_tokens is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import unittest | ||
|
||
from utils import get_api_key | ||
|
||
import cohere | ||
from cohere.responses import LogLikelihoods | ||
|
||
TEST_MODEL = "command-light" | ||
API_KEY = get_api_key() | ||
co = cohere.Client(API_KEY) | ||
|
||
|
||
class TestEmbed(unittest.TestCase): | ||
def test_basic_llh(self): | ||
resp = co.loglikelihood(model=TEST_MODEL, prompt="co:here", completion="co:where?") | ||
assert isinstance(resp, LogLikelihoods) | ||
assert isinstance(resp.prompt_tokens[0].encoded, int) |