llmir 0.0.10__py3-none-any.whl → 0.0.12__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.
llmir/adapter/openai.py CHANGED
@@ -1,6 +1,6 @@
1
1
  from typing import TypedDict, Literal, Required
2
2
 
3
- from ..messages import AIMessages, AIMessageToolResponse
3
+ from ..messages import AIMessages, AIMessageToolResponse, AIRoles
4
4
  from ..chunks import AIChunkText, AIChunkImageURL, AIChunkFile, AIChunkToolCall
5
5
  import base64
6
6
  import json
@@ -33,7 +33,7 @@ OpenAIContents = OpenAIText | OpenAIImageURL
33
33
 
34
34
 
35
35
  class OpenAIMessage(TypedDict, total=False):
36
- role: Required[str]
36
+ role: Required[Literal["system", "user", "assistant"]]
37
37
  content: list[OpenAIContents]
38
38
  tool_calls: list[OpenAIToolCall]
39
39
 
@@ -81,21 +81,44 @@ def to_openai(messages: list[AIMessages]) -> list[OpenAIMessages]:
81
81
  )
82
82
  )
83
83
  else:
84
- formatted = OpenAIMessage(
85
- role=role
84
+
85
+ assert(role != "tool")
86
+ content_chunks: list[AIChunkText | AIChunkFile | AIChunkImageURL] = []
87
+ tool_call_chunks: list[AIChunkToolCall] = []
88
+
89
+ media_chunks: list[AIChunkFile | AIChunkImageURL] = []
90
+
91
+ for chunk in message.chunks:
92
+
93
+ if isinstance(chunk, AIChunkToolCall):
94
+ tool_call_chunks.append(chunk)
95
+
96
+ elif role != AIRoles.USER and not isinstance(chunk, AIChunkText):
97
+ media_chunks.append(chunk)
98
+
99
+ else:
100
+ content_chunks.append(chunk)
101
+
102
+
103
+ if content_chunks or tool_call_chunks:
104
+ formatted = OpenAIMessage(
105
+ role=role
106
+ )
107
+ if content_chunks:
108
+ formatted["content"] = [content_chunk_to_openai(c) for c in content_chunks]
109
+ if tool_call_chunks:
110
+ formatted["tool_calls"] = [tool_call_chunk_to_openai(c) for c in tool_call_chunks]
111
+
112
+ result.append(formatted)
113
+
114
+ if media_chunks:
115
+ result.append(OpenAIMessage(
116
+ role="user", # Hacky too, but what else to circumvent API limitations in a broadly compatible way?
117
+ content=[
118
+ content_chunk_to_openai(c) for c in media_chunks
119
+ ]
120
+ )
86
121
  )
87
- content: list[OpenAIContents] = [
88
- content_chunk_to_openai(chunk) for chunk in message.chunks if not isinstance(chunk, AIChunkToolCall)
89
- ]
90
- tool_calls: list[OpenAIToolCall] = [
91
- tool_call_chunk_to_openai(chunk) for chunk in message.chunks if isinstance(chunk, AIChunkToolCall)
92
- ]
93
- if content:
94
- formatted["content"] = content
95
- if tool_calls:
96
- formatted["tool_calls"] = tool_calls
97
-
98
- result.append(formatted)
99
122
 
100
123
  return result
101
124
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: llmir
3
- Version: 0.0.10
3
+ Version: 0.0.12
4
4
  Summary: Core message and tool IR for LLM pipelines
5
5
  Author: Mathis Siebert
6
6
  Requires-Python: >=3.11
@@ -6,8 +6,8 @@ llmir/rich_repr.py,sha256=ZGAfpcPkYJSUpFdir8B1MoORVFBSzVckCrRHVVkJIj4,559
6
6
  llmir/roles.py,sha256=B9PmxTHAb8XoiA9en_J1yIP19woddzx4SRUz5XkUxFc,133
7
7
  llmir/tools.py,sha256=fpkC3IafHqZJnwo7imtHdkTdGLBHQuR6DtdztXZrlWg,147
8
8
  llmir/adapter/__init__.py,sha256=6yYVuYUXWiPPx5-v9PdSojEEw0TSFKz_4ELK9ntpRCw,437
9
- llmir/adapter/openai.py,sha256=mBoMgznljHOW05vAQu5Gv4p-eTUAD7YyPIypJZ7R8kQ,4622
10
- llmir-0.0.10.dist-info/METADATA,sha256=jZ9YcuNGqQRnkkUjzne3wJrA7m32hh5DD-i-sQ1Z3IM,178
11
- llmir-0.0.10.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
12
- llmir-0.0.10.dist-info/top_level.txt,sha256=fm538OsNUSYa_71IPztSSN2PBCvcbaD5PmUIzaO5hQs,6
13
- llmir-0.0.10.dist-info/RECORD,,
9
+ llmir/adapter/openai.py,sha256=gXPh_4kjF_hgxXCCjROQXRriDPQtECVUhVW0M3pYl84,5470
10
+ llmir-0.0.12.dist-info/METADATA,sha256=nLPChWuQr3M6er0dvA2mS0pNQMt6-lcZVW9b2gdNFYo,178
11
+ llmir-0.0.12.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
12
+ llmir-0.0.12.dist-info/top_level.txt,sha256=fm538OsNUSYa_71IPztSSN2PBCvcbaD5PmUIzaO5hQs,6
13
+ llmir-0.0.12.dist-info/RECORD,,
File without changes