mirror of
https://github.com/onyx-dot-app/onyx.git
synced 2026-10-23 03:42:59 -05:00
* ran bump-pydantic * replace root_validator with model_validator * mostly working. some alternate assistant error. changed root_validator and typing_extensions * working generation chat. changed type * replacing .dict with .model_dump * argument needed to bring model_dump up to parity with dict() * fix a fewremaining issues -- working with llama and gpt * updating requirements file * more requirement updates * more requirement updates * fix to make search work * return type fix: * half way tpyes change * fixes for mypy and pydantic: * endpoint fix * fix pydantic protected namespaces * it works! * removed unecessary None initializations * better logging * changed default values to empty lists * mypy fixes * fixed array defaulting --------- Co-authored-by: hagen-danswer <hagen@danswer.ai>
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import json
|
|
from typing import Any
|
|
|
|
from langchain_core.messages.ai import AIMessage
|
|
from langchain_core.messages.tool import ToolCall
|
|
from langchain_core.messages.tool import ToolMessage
|
|
from pydantic.v1 import BaseModel as BaseModel__v1
|
|
|
|
from danswer.natural_language_processing.utils import BaseTokenizer
|
|
|
|
# Langchain has their own version of pydantic which is version 1
|
|
|
|
|
|
def build_tool_message(
|
|
tool_call: ToolCall, tool_content: str | list[str | dict[str, Any]]
|
|
) -> ToolMessage:
|
|
return ToolMessage(
|
|
tool_call_id=tool_call["id"] or "",
|
|
name=tool_call["name"],
|
|
content=tool_content,
|
|
)
|
|
|
|
|
|
class ToolCallSummary(BaseModel__v1):
|
|
tool_call_request: AIMessage
|
|
tool_call_result: ToolMessage
|
|
|
|
|
|
def tool_call_tokens(
|
|
tool_call_summary: ToolCallSummary, llm_tokenizer: BaseTokenizer
|
|
) -> int:
|
|
request_tokens = len(
|
|
llm_tokenizer.encode(
|
|
json.dumps(tool_call_summary.tool_call_request.tool_calls[0]["args"])
|
|
)
|
|
)
|
|
result_tokens = len(
|
|
llm_tokenizer.encode(json.dumps(tool_call_summary.tool_call_result.content))
|
|
)
|
|
|
|
return request_tokens + result_tokens
|