-
Notifications
You must be signed in to change notification settings - Fork 86
Seperate files from __init__.py
#13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f6216e
Seperate files from
jimwu6 63ca949
fix version again
jimwu6 31fc427
include import for easier user access
jimwu6 232ab75
Merge branch 'main' into restruct-files
jimwu6 a42cec7
Update __init__.py
jimwu6 c4f5207
Make more formatting changes and update sanity-test.py
jimwu6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,144 +1,9 @@ | ||
| import json | ||
| from typing import List | ||
| from urllib.parse import urljoin | ||
|
|
||
| import requests | ||
| from requests import Response | ||
| from .client import CohereClient | ||
| from .error import CohereError | ||
|
|
||
| COHERE_API_URL = "https://api.cohere.ai" | ||
| GENERATE_URL = "generate" | ||
| SIMILARITY_URL = "similarity" | ||
| EMBED_URL = "embed" | ||
| CHOOSE_BEST_URL = "choose-best" | ||
| LIKELIHOOD_URL = "likelihood" | ||
|
|
||
| class Generation: | ||
| def __init__(self, text) -> None: | ||
| self.text = text | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.text | ||
|
|
||
| class Similarities: | ||
| def __init__(self, similarities) -> None: | ||
| self.similarities = similarities | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.similarities) | ||
|
|
||
| class Embeddings: | ||
| def __init__(self, embeddings) -> None: | ||
| self.embeddings = embeddings | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.embeddings) | ||
|
|
||
| class BestChoices: | ||
| def __init__(self, likelihoods, mode) -> None: | ||
| self.likelihoods = likelihoods | ||
| self.mode = mode | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.likelihoods) | ||
|
|
||
| class Likelihoods: | ||
| def __init__(self, likelihood, token_likelihoods) -> None: | ||
| self.likelihood = likelihood | ||
| self.token_likelihoods = token_likelihoods | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.likelihood) + "\n" + str(self.token_likelihoods) | ||
|
|
||
| class CohereClient: | ||
| def __init__(self, api_key: str) -> None: | ||
| self.api_key = api_key | ||
| self.api_url = COHERE_API_URL | ||
|
|
||
| def generate( | ||
| self, | ||
| model: str, | ||
| prompt: str, | ||
| max_tokens: int = 20, | ||
| temperature: float = 1.0, | ||
| k: int = 0, p: float = 0.75 | ||
| ) -> Generation: | ||
| json_body = json.dumps({ | ||
| "prompt": prompt, | ||
| "max_tokens": max_tokens, | ||
| "temperature": temperature, | ||
| "k": k, | ||
| "p": p, | ||
| }) | ||
| response = self.__request(json_body, GENERATE_URL, model) | ||
| return Generation(response["text"]) | ||
|
|
||
| def similarity(self, model: str, anchor: str, targets: List[str]) -> Similarities: | ||
| json_body = json.dumps({ | ||
| "anchor": anchor, | ||
| "targets": targets, | ||
| }) | ||
| response = self.__request(json_body, SIMILARITY_URL, model) | ||
| return Similarities(response["similarities"]) | ||
|
|
||
| def embed(self, model: str, texts: List[str]) -> Embeddings: | ||
| json_body = json.dumps({ | ||
| "texts": texts, | ||
| }) | ||
| response = self.__request(json_body, EMBED_URL, model) | ||
| return Embeddings(response["embeddings"]) | ||
|
|
||
| def choose_best(self, model: str, query: str, options: List[str], mode: str = "") -> BestChoices: | ||
| json_body = json.dumps({ | ||
| "query": query, | ||
| "options": options, | ||
| "mode": mode, | ||
| }) | ||
| response = self.__request(json_body, CHOOSE_BEST_URL, model) | ||
| return BestChoices(response['likelihoods'], mode) | ||
|
|
||
| def likelihood(self, model: str, text: List[str]) -> Likelihoods: | ||
| json_body = json.dumps({ | ||
| "text": text, | ||
| }) | ||
| response = self.__request(json_body, LIKELIHOOD_URL, model) | ||
| return Likelihoods(response['likelihood'], response['token_likelihoods']) | ||
|
|
||
| def __request(self, json_body, endpoint, model) -> Response: | ||
| headers = { | ||
| 'Authorization': 'BEARER {}'.format(self.api_key), | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| url = urljoin(self.api_url, model + "/" + endpoint) | ||
| response = requests.request("POST", url, headers=headers, data=json_body) | ||
| res = json.loads(response.text) | ||
| if "message" in res.keys(): # has errors | ||
| raise CohereError( | ||
| message=res["message"], | ||
| http_status=response.status_code, | ||
| headers=response.headers) | ||
| return res | ||
|
|
||
| class CohereError(Exception): | ||
| def __init__( | ||
| self, | ||
| message=None, | ||
| http_status=None, | ||
| headers=None, | ||
| ) -> None: | ||
| super(CohereError, self).__init__(message) | ||
|
|
||
| self.message = message | ||
| self.http_status = http_status | ||
| self.headers = headers or {} | ||
|
|
||
| def __str__(self) -> str: | ||
| msg = self.message or "<empty message>" | ||
| return msg | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "%s(message=%r, http_status=%r, request_id=%r)" % ( | ||
| self.__class__.__name__, | ||
| self.message, | ||
| self.http_status, | ||
| ) | ||
|
|
This file contains hidden or 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,9 @@ | ||
| from typing import List | ||
|
|
||
| class BestChoices: | ||
| def __init__(self, likelihoods: List[float], mode: str) -> None: | ||
| self.likelihoods = likelihoods | ||
| self.mode = mode | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.likelihoods) |
This file contains hidden or 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,85 @@ | ||
| import json | ||
| from urllib.parse import urljoin | ||
|
|
||
| from typing import List | ||
| import requests | ||
| from requests import Response | ||
|
|
||
| import cohere | ||
| from cohere.error import CohereError | ||
|
|
||
| from cohere.generation import Generation | ||
| from cohere.similarities import Similarities | ||
| from cohere.embeddings import Embeddings | ||
| from cohere.best_choices import BestChoices | ||
| from cohere.likelihoods import Likelihoods | ||
|
|
||
|
|
||
| class CohereClient: | ||
| def __init__(self, api_key: str) -> None: | ||
| self.api_key = api_key | ||
| self.api_url = cohere.COHERE_API_URL | ||
|
|
||
| def generate( | ||
| self, | ||
| model: str, | ||
| prompt: str, | ||
| max_tokens: int = 20, | ||
| temperature: float = 1.0, | ||
| k: int = 0, p: float = 0.75 | ||
| ) -> Generation: | ||
| json_body = json.dumps({ | ||
| "prompt": prompt, | ||
| "max_tokens": max_tokens, | ||
| "temperature": temperature, | ||
| "k": k, | ||
| "p": p, | ||
| }) | ||
| response = self.__request(json_body, cohere.GENERATE_URL, model) | ||
| return Generation(response["text"]) | ||
|
|
||
| def similarity(self, model: str, anchor: str, targets: List[str]) -> Similarities: | ||
| json_body = json.dumps({ | ||
| "anchor": anchor, | ||
| "targets": targets, | ||
| }) | ||
| response = self.__request(json_body, cohere.SIMILARITY_URL, model) | ||
| return Similarities(response["similarities"]) | ||
|
|
||
| def embed(self, model: str, texts: List[str]) -> Embeddings: | ||
| json_body = json.dumps({ | ||
| "texts": texts, | ||
| }) | ||
| response = self.__request(json_body, cohere.EMBED_URL, model) | ||
| return Embeddings(response["embeddings"]) | ||
|
|
||
| def choose_best(self, model: str, query: str, options: List[str], mode: str = "") -> BestChoices: | ||
| json_body = json.dumps({ | ||
| "query": query, | ||
| "options": options, | ||
| "mode": mode, | ||
| }) | ||
| response = self.__request(json_body, cohere.CHOOSE_BEST_URL, model) | ||
| return BestChoices(response['likelihoods'], mode) | ||
|
|
||
| def likelihood(self, model: str, text: List[str]) -> Likelihoods: | ||
| json_body = json.dumps({ | ||
| "text": text, | ||
| }) | ||
| response = self.__request(json_body, cohere.LIKELIHOOD_URL, model) | ||
| return Likelihoods(response['likelihood'], response['token_likelihoods']) | ||
|
|
||
| def __request(self, json_body, endpoint, model) -> Response: | ||
| headers = { | ||
| 'Authorization': 'BEARER {}'.format(self.api_key), | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| url = urljoin(self.api_url, model + "/" + endpoint) | ||
| response = requests.request("POST", url, headers=headers, data=json_body) | ||
| res = json.loads(response.text) | ||
| if "message" in res.keys(): # has errors | ||
| raise CohereError( | ||
| message=res["message"], | ||
| http_status=response.status_code, | ||
| headers=response.headers) | ||
| return res |
This file contains hidden or 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,8 @@ | ||
| from typing import List | ||
|
|
||
| class Embeddings: | ||
| def __init__(self, embeddings: List[List[float]]) -> None: | ||
| self.embeddings = embeddings | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.embeddings) |
This file contains hidden or 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,23 @@ | ||
| class CohereError(Exception): | ||
| def __init__( | ||
| self, | ||
| message=None, | ||
| http_status=None, | ||
| headers=None, | ||
| ) -> None: | ||
| super(CohereError, self).__init__(message) | ||
|
|
||
| self.message = message | ||
| self.http_status = http_status | ||
| self.headers = headers or {} | ||
|
|
||
| def __str__(self) -> str: | ||
| msg = self.message or "<empty message>" | ||
| return msg | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "%s(message=%r, http_status=%r, request_id=%r)" % ( | ||
| self.__class__.__name__, | ||
| self.message, | ||
| self.http_status, | ||
| ) |
This file contains hidden or 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,6 @@ | ||
| class Generation: | ||
| def __init__(self, text: str) -> None: | ||
| self.text = text | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.text |
This file contains hidden or 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,9 @@ | ||
| from typing import List, Dict | ||
|
|
||
| class Likelihoods: | ||
| def __init__(self, likelihood: float, token_likelihoods: List[Dict]) -> None: | ||
| self.likelihood = likelihood | ||
| self.token_likelihoods = token_likelihoods | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.likelihood) + "\n" + str(self.token_likelihoods) |
This file contains hidden or 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,8 @@ | ||
| from typing import List | ||
|
|
||
| class Similarities: | ||
| def __init__(self, similarities: List[float]) -> None: | ||
| self.similarities = similarities | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self.similarities) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.