Skip to content

Commit

Permalink
feat: Added support for moderations in generation_info
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszOssGit committed Nov 18, 2024
1 parent 3b34b94 commit 1810726
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 91 deletions.
8 changes: 7 additions & 1 deletion libs/ibm/langchain_ibm/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,13 @@ def _create_llm_result(self, response: List[dict]) -> LLMResult:
results = res.get("results")
if results:
finish_reason = results[0].get("stop_reason")
moderations = results[0].get("moderations")
gen = Generation(
text=results[0].get("generated_text"),
generation_info={"finish_reason": finish_reason},
generation_info={
"finish_reason": finish_reason,
"moderations": moderations,
},
)
generations.append([gen])
final_token_usage = self._extract_token_usage(response)
Expand All @@ -345,13 +349,15 @@ def _stream_response_to_generation_chunk(
return GenerationChunk(text="")

finish_reason = stream_response["results"][0].get("stop_reason", None)
moderations = stream_response["results"][0].get("moderations", None)

return GenerationChunk(
text=stream_response["results"][0]["generated_text"],
generation_info=dict(
finish_reason=(
None if finish_reason == "not_finished" else finish_reason
),
moderations=moderations,
llm_output={
"model_id": self.model_id,
"deployment_id": self.deployment_id,
Expand Down
Loading

0 comments on commit 1810726

Please sign in to comment.