Skip to content

Commit

Permalink
Merge pull request #92 from Aleph-Alpha/make-json-deserialization-for…
Browse files Browse the repository at this point in the history
…ward-compatible

Make json deserialization forward compatible
  • Loading branch information
volkerstampa authored Feb 17, 2023
2 parents 07f8322 + 86493be commit 0c7b89a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 13 additions & 1 deletion aleph_alpha_client/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ class CompletionResult(NamedTuple):
finish_reason: Optional[str] = None
raw_completion: Optional[str] = None

@staticmethod
def from_json(json: Dict[str, Any]) -> "CompletionResult":
return CompletionResult(
log_probs=json.get("log_probs"),
completion=json.get("completion"),
completion_tokens=json.get("completion_tokens"),
finish_reason=json.get("finish_reason"),
raw_completion=json.get("raw_completion"),
)


class CompletionResponse(NamedTuple):
model_version: str
Expand All @@ -228,7 +238,9 @@ class CompletionResponse(NamedTuple):
def from_json(json: Dict[str, Any]) -> "CompletionResponse":
return CompletionResponse(
model_version=json["model_version"],
completions=[CompletionResult(**item) for item in json["completions"]],
completions=[
CompletionResult.from_json(item) for item in json["completions"]
],
optimized_prompt=json.get("optimized_prompt"),
)

Expand Down
2 changes: 1 addition & 1 deletion aleph_alpha_client/detokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class DetokenizationResponse(NamedTuple):

@staticmethod
def from_json(json: Dict[str, Any]) -> "DetokenizationResponse":
return DetokenizationResponse(**json)
return DetokenizationResponse(result=json["result"])
6 changes: 5 additions & 1 deletion aleph_alpha_client/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class SearchResult(NamedTuple):
id: str
score: float

@staticmethod
def from_json(json: Dict[str, Any]) -> "SearchResult":
return SearchResult(id=json["id"], score=json["score"])


class SearchResponse(NamedTuple):
model_version: str
Expand All @@ -67,5 +71,5 @@ class SearchResponse(NamedTuple):
def from_json(json: Dict[str, Any]) -> "SearchResponse":
return SearchResponse(
model_version=json["model_version"],
results=[SearchResult(**item) for item in json["results"]],
results=[SearchResult.from_json(item) for item in json["results"]],
)

0 comments on commit 0c7b89a

Please sign in to comment.