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

community: Add token metadata to ChatYandexGPT response #24182

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c48b7a9
added response_metadata to yandexchatgpt answer
olgamurraft Jul 12, 2024
3a1e212
code check
olgamurraft Jul 12, 2024
366ca2b
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Jul 16, 2024
c32dbe3
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Jul 18, 2024
df73335
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Jul 22, 2024
c6e77f9
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Jul 23, 2024
72193be
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 5, 2024
0885354
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 13, 2024
5c68426
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 14, 2024
a59a21f
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 16, 2024
af6d88c
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 20, 2024
e820306
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 23, 2024
4f1ed30
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 26, 2024
c05eacd
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 28, 2024
431a82c
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
efriis Aug 28, 2024
9dc4e53
x
efriis Aug 28, 2024
fde9d83
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Aug 29, 2024
865d574
failed lint
olgamurraft Sep 4, 2024
2d226c4
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Sep 4, 2024
68ab350
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Sep 12, 2024
0a5b2c6
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Sep 17, 2024
6679e91
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Sep 24, 2024
84e6173
Merge branch 'master' into add-token-metadata-to-yandex-llm-response
olgamurraft Sep 27, 2024
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
46 changes: 33 additions & 13 deletions libs/community/langchain_community/chat_models/yandex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
CallbackManagerForLLMRun,
)
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
SystemMessage,
)
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langchain_core.outputs import ChatGeneration, ChatResult
from tenacity import (
before_sleep_log,
Expand Down Expand Up @@ -92,8 +87,14 @@ def _generate(
"""
text = completion_with_retry(self, messages=messages)
text = text if stop is None else enforce_stop_tokens(text, stop)
message = AIMessage(content=text)
return ChatResult(generations=[ChatGeneration(message=message)])
message = AIMessage(
content=text,
response_metadata=self.metadata["llm_output"] if self.metadata else None,
)
return ChatResult(
generations=[ChatGeneration(message=message)],
llm_output=self.metadata["llm_output"] if self.metadata else None,
)

async def _agenerate(
self,
Expand All @@ -117,8 +118,14 @@ async def _agenerate(
"""
text = await acompletion_with_retry(self, messages=messages)
text = text if stop is None else enforce_stop_tokens(text, stop)
message = AIMessage(content=text)
return ChatResult(generations=[ChatGeneration(message=message)])
message = AIMessage(
content=text,
response_metadata=self.metadata["llm_output"] if self.metadata else None,
)
return ChatResult(
generations=[ChatGeneration(message=message)],
llm_output=self.metadata["llm_output"] if self.metadata else None,
)


def _make_request(
Expand All @@ -130,7 +137,7 @@ def _make_request(
from google.protobuf.wrappers_pb2 import DoubleValue, Int64Value

try:
from yandex.cloud.ai.foundation_models.v1.text_common_pb2 import (
from yandex.cloud.ai.foundation_models.v1.text_common_pb2 import ( # noqa: E501
CompletionOptions,
Message,
)
Expand Down Expand Up @@ -158,6 +165,8 @@ def _make_request(
) from e
if not messages:
raise ValueError("You should provide at least one message to start the chat!")
if self.metadata is None:
self.metadata = {}
message_history = _parse_chat_history(messages)
channel_credentials = grpc.ssl_channel_credentials()
channel = grpc.secure_channel(self.url, channel_credentials)
Expand All @@ -171,7 +180,12 @@ def _make_request(
)
stub = TextGenerationServiceStub(channel)
res = stub.Completion(request, metadata=self.grpc_metadata)
return list(res)[0].alternatives[0].message.text
list_res = list(res)
self.metadata["llm_output"] = {
"token_usage": list_res[0].usage,
"model_name": self.model_name,
}
return list_res[0].alternatives[0].message.text


async def _amake_request(self: ChatYandexGPT, messages: List[BaseMessage]) -> str:
Expand All @@ -182,7 +196,7 @@ async def _amake_request(self: ChatYandexGPT, messages: List[BaseMessage]) -> st
from google.protobuf.wrappers_pb2 import DoubleValue, Int64Value

try:
from yandex.cloud.ai.foundation_models.v1.text_common_pb2 import (
from yandex.cloud.ai.foundation_models.v1.text_common_pb2 import ( # noqa: E501
CompletionOptions,
Message,
)
Expand Down Expand Up @@ -219,6 +233,8 @@ async def _amake_request(self: ChatYandexGPT, messages: List[BaseMessage]) -> st
message_history = _parse_chat_history(messages)
operation_api_url = "operation.api.cloud.yandex.net:443"
channel_credentials = grpc.ssl_channel_credentials()
if self.metadata is None:
self.metadata = {}
async with grpc.aio.secure_channel(self.url, channel_credentials) as channel:
request = CompletionRequest(
model_uri=self.model_uri,
Expand All @@ -244,6 +260,10 @@ async def _amake_request(self: ChatYandexGPT, messages: List[BaseMessage]) -> st

completion_response = CompletionResponse()
operation.response.Unpack(completion_response)
self.metadata["llm_output"] = {
"token_usage": completion_response.usage,
"model_name": self.model_name,
}
return completion_response.alternatives[0].message.text


Expand Down