Skip to content

Commit 7e36297

Browse files
authored
langchain callback improve message init (#5)
* callback modify message init * update tool span type * modify tracespec package name
1 parent a4ee397 commit 7e36297

21 files changed

Lines changed: 36 additions & 25 deletions

File tree

cozeloop/integration/langchain/trace_callback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def _span_type_mapping(span_type: str) -> str:
304304
elif span_type == 'ReActSingleInputOutputParser':
305305
return 'parser'
306306
elif span_type == 'tool':
307-
return 'plugin'
307+
return 'tool'
308308
return span_type
309309

310310

@@ -406,7 +406,7 @@ def _convert_inputs(inputs: Any) -> Any:
406406
format_inputs['content'] = inputs.content
407407
return format_inputs
408408
if isinstance(inputs, BaseMessage):
409-
message = Message(role=inputs.type, content=inputs.content)
409+
message = Message(role=inputs.type, content=inputs.content, tool_calls=inputs.additional_kwargs.get('tool_calls', []))
410410
return message
411411
if isinstance(inputs, ChatPromptValue):
412412
return _convert_inputs(inputs.messages)

cozeloop/integration/langchain/trace_model/llm_model.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import json
55
import time
6-
from typing import List, Optional, Union
6+
from typing import List, Optional, Union, Dict, Any
77
from pydantic.dataclasses import dataclass
88
from langchain_core.messages import BaseMessage, ToolMessage, AIMessageChunk
99
from langchain_core.outputs import Generation, ChatGeneration
@@ -14,7 +14,7 @@ class ToolFunction:
1414
name: Optional[str] = None
1515
description: Optional[str] = None
1616
parameters: Optional[dict] = None
17-
arguments: Optional[dict] = None
17+
arguments: Optional[Union[dict, str]] = None
1818

1919

2020
@dataclass
@@ -50,20 +50,31 @@ class Message:
5050
tool_calls: List[ToolCall] = None
5151

5252
def __post_init__(self):
53-
if self.role is not None and self.role == 'AIMessageChunk':
53+
if self.role is not None and (self.role == 'AIMessageChunk' or self.role == 'ai'):
5454
self.role = 'assistant'
5555
parts: Optional[List[Parts]] = []
5656
if isinstance(self.content, List) and all(isinstance(x, dict) for x in self.content):
57+
is_parts = False
5758
for each in self.content:
5859
text = each.get('text', None)
5960
url = each.get('url', each.get('image_url', {}).get('url', None))
61+
if text is None and url is None:
62+
continue
63+
is_parts = True
6064
parts.append(Parts(type=each.get('type', ''), text=text, image_url=ImageUrl(url=url) if url is not None else None))
61-
self.content = None
65+
if is_parts:
66+
self.content = None
67+
else:
68+
self.content = self.content.__str__()
6269
elif isinstance(self.content, dict):
70+
is_part = False
6371
text = self.content.get('text', None)
6472
url = self.content.get('url', self.content.get('image_url', {}).get('url', None))
65-
parts.append(Parts(type=self.content.get('type', ''), text=text, image_url=ImageUrl(url=url) if url is not None else None))
66-
self.content = None
73+
if text is not None or url is not None:
74+
parts.append(Parts(type=self.content.get('type', ''), text=text, image_url=ImageUrl(url=url) if url is not None else None))
75+
self.content = None
76+
else:
77+
self.content = self.content.__str__()
6778
elif isinstance(self.content, List) and all(type(x, Parts) for x in self.content):
6879
parts = self.content
6980
self.content = None
@@ -172,4 +183,4 @@ def convert_tool_calls(tool_calls: list) -> List[ToolCall]:
172183
for tool_call in tool_calls:
173184
function = ToolFunction(name=tool_call.get('function', {}).get('name', ''), arguments=json.loads(tool_call.get('function', {}).get('arguments', {})))
174185
format_tool_calls.append(ToolCall(id=tool_call.get('id', ''), type=tool_call.get('type', ''), function=function))
175-
return format_tool_calls
186+
return format_tool_calls

cozeloop/integration/langchain/trace_model/runtime.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import importlib.metadata as metadata
66
from typing import Optional, Any
77

8-
from cozeloop.spec import tracespce
8+
from cozeloop.spec import tracespec
99

1010

11-
class RuntimeInfo(tracespce.Runtime):
12-
language: Optional[str] = tracespce.V_LANG_PYTHON
13-
library: Optional[str] = tracespce.V_LIB_LANGCHAIN
11+
class RuntimeInfo(tracespec.Runtime):
12+
language: Optional[str] = tracespec.V_LANG_PYTHON
13+
library: Optional[str] = tracespec.V_LIB_LANGCHAIN
1414

1515
def model_post_init(self, context: Any) -> None:
1616
try:

cozeloop/internal/consts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from contextvars import ContextVar
55

66
from .error import *
7-
from ...spec.tracespce.span_key import INPUT_TOKENS, OUTPUT_TOKENS, TOKENS, INPUT, OUTPUT
7+
from ...spec.tracespec.span_key import INPUT_TOKENS, OUTPUT_TOKENS, TOKENS, INPUT, OUTPUT
88

99
# default values for loop client
1010
# ComBaseURL = "https://api.coze.com"

cozeloop/internal/prompt/converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing import List, Dict
55

6-
from cozeloop.spec.tracespce import PromptInput, PromptOutput, ModelMessage, PromptArgument
6+
from cozeloop.spec.tracespec import PromptInput, PromptOutput, ModelMessage, PromptArgument
77
from cozeloop.entities.prompt import (
88
Prompt as EntityPrompt,
99
Message as EntityMessage,

cozeloop/internal/prompt/prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from jinja2 import Environment, BaseLoader, Undefined
88
from jinja2.utils import missing, object_type_repr
99

10-
from cozeloop.spec.tracespce import PROMPT_KEY, INPUT, PROMPT_VERSION, V_SCENE_PROMPT_TEMPLATE, V_SCENE_PROMPT_HUB
10+
from cozeloop.spec.tracespec import PROMPT_KEY, INPUT, PROMPT_VERSION, V_SCENE_PROMPT_TEMPLATE, V_SCENE_PROMPT_HUB
1111
from cozeloop.entities.prompt import (Prompt, Message, VariableDef, VariableType, TemplateType, Role,
1212
PromptVariable)
1313
from cozeloop.internal import consts

cozeloop/internal/trace/exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77
from typing import Dict, List, Optional, Tuple, Callable, Any
88

9-
from cozeloop.spec.tracespce import ModelInput, ModelMessagePart, ModelMessagePartType, ModelImageURL, ModelFileURL, ModelOutput
9+
from cozeloop.spec.tracespec import ModelInput, ModelMessagePart, ModelMessagePartType, ModelImageURL, ModelFileURL, ModelOutput
1010
from cozeloop.internal.consts import *
1111
from cozeloop.internal.httpclient import Client, BaseResponse
1212
from cozeloop.internal.trace.model.model import UploadType, Attachment, ObjectStorage

cozeloop/internal/trace/noop_span.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from cozeloop.entities.prompt import Prompt
1010
from cozeloop.span import Span
11-
from cozeloop.spec.tracespce import Runtime
11+
from cozeloop.spec.tracespec import Runtime
1212

1313

1414
class NoopSpan(Span, ABC):

cozeloop/internal/trace/span.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import urllib.parse
1111

1212
from cozeloop import span
13-
from cozeloop.spec.tracespce import (ModelInput, ModelOutput, ModelMessagePartType, ModelMessage, ModelMessagePart,
13+
from cozeloop.spec.tracespec import (ModelInput, ModelOutput, ModelMessagePartType, ModelMessage, ModelMessagePart,
1414
ModelImageURL, ModelFileURL, ModelChoice, Runtime, ERROR, PROMPT_KEY,
1515
PROMPT_VERSION, MODEL_PROVIDER, MODEL_NAME, RUNTIME_, CALL_OPTIONS,
1616
V_SCENE_CUSTOM, V_LANG_PYTHON)

cozeloop/internal/trace/trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from datetime import datetime
66
from typing import Dict, Optional
77

8-
from cozeloop.spec.tracespce import Runtime, RUNTIME_
8+
from cozeloop.spec.tracespec import Runtime, RUNTIME_
99
from cozeloop.internal import consts
1010
from cozeloop.internal.trace.span import from_header, Span, SpanContext, \
1111
get_newest_span_from_context, set_span_to_context, logger

0 commit comments

Comments
 (0)