langchain-b12 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_b12/genai/genai.py +8 -7
- langchain_b12/genai/genai_utils.py +66 -15
- {langchain_b12-0.1.0.dist-info → langchain_b12-0.1.2.dist-info}/METADATA +1 -3
- langchain_b12-0.1.2.dist-info/RECORD +8 -0
- langchain_b12-0.1.0.dist-info/RECORD +0 -8
- {langchain_b12-0.1.0.dist-info → langchain_b12-0.1.2.dist-info}/WHEEL +0 -0
langchain_b12/genai/genai.py
CHANGED
|
@@ -145,13 +145,14 @@ class ChatGenAI(BaseChatModel):
|
|
|
145
145
|
self, messages: list[BaseMessage]
|
|
146
146
|
) -> tuple[str | None, types.ContentListUnion]:
|
|
147
147
|
contents = convert_messages_to_contents(messages)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
system_instruction
|
|
148
|
+
system_message: SystemMessage | None = next(
|
|
149
|
+
(message for message in messages if isinstance(message, SystemMessage)),
|
|
150
|
+
None,
|
|
151
|
+
)
|
|
152
|
+
system_instruction = system_message.content if system_message else None
|
|
153
|
+
assert system_instruction is None or isinstance(
|
|
154
|
+
system_instruction, str
|
|
155
|
+
), "System message content must be a string or None"
|
|
155
156
|
return system_instruction, cast(types.ContentListUnion, contents)
|
|
156
157
|
|
|
157
158
|
def get_num_tokens(self, text: str) -> int:
|
|
@@ -24,22 +24,37 @@ def multi_content_to_part(
|
|
|
24
24
|
Args:
|
|
25
25
|
contents: A sequence of dictionaries representing content. Examples:
|
|
26
26
|
[
|
|
27
|
-
{
|
|
27
|
+
{ # Text content
|
|
28
28
|
"type": "text",
|
|
29
29
|
"text": "This is a text message"
|
|
30
30
|
},
|
|
31
|
-
{
|
|
31
|
+
{ # Image content from base64 encoded string with OpenAI format
|
|
32
32
|
"type": "image_url",
|
|
33
33
|
"image_url": {
|
|
34
34
|
"url": f"data:{mime_type};base64,{encoded_artifact}"
|
|
35
35
|
},
|
|
36
36
|
},
|
|
37
|
-
{
|
|
37
|
+
{ # Image content fro base64 encoded string with LangChain format
|
|
38
|
+
"type": "image",
|
|
39
|
+
"source_type": "base64",
|
|
40
|
+
"data": "<base64 string>",
|
|
41
|
+
"mime_type": "image/jpeg",
|
|
42
|
+
},
|
|
43
|
+
{ # Image content from URL
|
|
44
|
+
"type": "image",
|
|
45
|
+
"source_type": "url",
|
|
46
|
+
"url": "https://...",
|
|
47
|
+
},
|
|
48
|
+
{ # File content from base64 encoded string
|
|
49
|
+
"type": "file",
|
|
50
|
+
"source_type": "base64",
|
|
51
|
+
"mime_type": "application/pdf",
|
|
52
|
+
"data": "<base64 data string>",
|
|
53
|
+
},
|
|
54
|
+
{ # File content from URL
|
|
38
55
|
"type": "file",
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
"mime_type": mime_type,
|
|
42
|
-
}
|
|
56
|
+
"source_type": "url",
|
|
57
|
+
"url": "https://...",
|
|
43
58
|
}
|
|
44
59
|
]
|
|
45
60
|
"""
|
|
@@ -60,15 +75,51 @@ def multi_content_to_part(
|
|
|
60
75
|
mime_type = header.split(":", 1)[1].split(";", 1)[0]
|
|
61
76
|
data = base64.b64decode(encoded_data)
|
|
62
77
|
parts.append(types.Part.from_bytes(data=data, mime_type=mime_type))
|
|
78
|
+
elif content["type"] == "image":
|
|
79
|
+
if "data" in content:
|
|
80
|
+
assert isinstance(content["data"], str), "Expected str data"
|
|
81
|
+
assert "mime_type" in content, "Expected 'mime_type' in content"
|
|
82
|
+
assert isinstance(content["mime_type"], str), "Expected str mime_type"
|
|
83
|
+
data = base64.b64decode(content["data"])
|
|
84
|
+
parts.append(
|
|
85
|
+
types.Part.from_bytes(data=data, mime_type=content["mime_type"])
|
|
86
|
+
)
|
|
87
|
+
elif "url" in content:
|
|
88
|
+
assert isinstance(content["url"], str), "Expected str url"
|
|
89
|
+
mime_type = content.get("mime_type", None)
|
|
90
|
+
assert mime_type is None or isinstance(
|
|
91
|
+
mime_type, str
|
|
92
|
+
), "Expected str mime_type"
|
|
93
|
+
parts.append(
|
|
94
|
+
types.Part.from_uri(file_uri=content["url"], mime_type=mime_type)
|
|
95
|
+
)
|
|
96
|
+
else:
|
|
97
|
+
raise ValueError(
|
|
98
|
+
"Expected either 'data' or 'url' in content for image type"
|
|
99
|
+
)
|
|
63
100
|
elif content["type"] == "file":
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
101
|
+
if "data" in content:
|
|
102
|
+
assert isinstance(content["data"], str), "Expected str data"
|
|
103
|
+
assert "mime_type" in content, "Expected 'mime_type' in content"
|
|
104
|
+
assert isinstance(content["mime_type"], str), "Expected str mime_type"
|
|
105
|
+
data = base64.b64decode(content["data"])
|
|
106
|
+
parts.append(
|
|
107
|
+
types.Part.from_bytes(data=data, mime_type=content["mime_type"])
|
|
108
|
+
)
|
|
109
|
+
elif "url" in content:
|
|
110
|
+
assert isinstance(content["url"], str), "Expected str url"
|
|
111
|
+
assert content["url"], "File URI is required"
|
|
112
|
+
mime_type = content.get("mime_type", None)
|
|
113
|
+
assert mime_type is None or isinstance(
|
|
114
|
+
mime_type, str
|
|
115
|
+
), "Expected str mime_type"
|
|
116
|
+
parts.append(
|
|
117
|
+
types.Part.from_uri(file_uri=content["url"], mime_type=mime_type)
|
|
118
|
+
)
|
|
119
|
+
else:
|
|
120
|
+
raise ValueError(
|
|
121
|
+
"Expected either 'data' or 'url' in content for file type"
|
|
122
|
+
)
|
|
72
123
|
else:
|
|
73
124
|
raise ValueError(f"Unknown content type: {content['type']}")
|
|
74
125
|
return parts
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langchain-b12
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A reusable collection of tools and implementations for Langchain
|
|
5
5
|
Author-email: Vincent Min <vincent.min@b12-consulting.com>
|
|
6
6
|
Requires-Python: >=3.11
|
|
@@ -11,8 +11,6 @@ Description-Content-Type: text/markdown
|
|
|
11
11
|
|
|
12
12
|
This repo hosts a collection of custom LangChain components.
|
|
13
13
|
|
|
14
|
-
- ChatGenAI converts a sequence of LangChain messages to Google GenAI Content objects and vice versa.
|
|
15
|
-
|
|
16
14
|
## Installation
|
|
17
15
|
|
|
18
16
|
To install this package, run
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
langchain_b12/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
langchain_b12/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
langchain_b12/genai/embeddings.py,sha256=od2bVIgt7v9aNAHG0PVypVF1H_XgHto2nTd8vwfvyN8,3355
|
|
4
|
+
langchain_b12/genai/genai.py,sha256=gzkgtvs3wNjcslS_KFZYCajUZIsJkVN2Tq2Q1RMIPyc,15910
|
|
5
|
+
langchain_b12/genai/genai_utils.py,sha256=La3FOtRGmzI5IDql3ojaYzz-lJqSgHOn0Wp3KwZ3P4I,10735
|
|
6
|
+
langchain_b12-0.1.2.dist-info/METADATA,sha256=8Uzn2ChQ8scEHLRiMykPTdP58wZEgiAaRXS8f2DjTe4,1204
|
|
7
|
+
langchain_b12-0.1.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
8
|
+
langchain_b12-0.1.2.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
langchain_b12/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
langchain_b12/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
-
langchain_b12/genai/embeddings.py,sha256=od2bVIgt7v9aNAHG0PVypVF1H_XgHto2nTd8vwfvyN8,3355
|
|
4
|
-
langchain_b12/genai/genai.py,sha256=o2KLo2QlLDy0hijt6T2HaRmjSO60SV62dR_Cso6Ad-8,15796
|
|
5
|
-
langchain_b12/genai/genai_utils.py,sha256=2ojnSumovXyx3CxM7JwzyOkEdD7mQxfLnk50-usPbw8,8221
|
|
6
|
-
langchain_b12-0.1.0.dist-info/METADATA,sha256=72Ct-UG2KHnPQuyUltOQ_HGpvLbDrXglmPn-eY9blnc,1307
|
|
7
|
-
langchain_b12-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
8
|
-
langchain_b12-0.1.0.dist-info/RECORD,,
|
|
File without changes
|