llmir 0.0.8__tar.gz → 0.0.10__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: llmir
3
- Version: 0.0.8
3
+ Version: 0.0.10
4
4
  Summary: Core message and tool IR for LLM pipelines
5
5
  Author: Mathis Siebert
6
6
  Requires-Python: >=3.11
@@ -1,4 +1,4 @@
1
- from .openai import to_openai, OpenAIMessages, OpenAIMessage, OpenAIMessageToolResponse, OpenAIContents, OpenAITextContent, OpenAIImageURLContent, OpenAIImageURLURL, OpenAIToolCallContent, OpenAIToolCallFunction
1
+ from .openai import to_openai, OpenAIMessages, OpenAIMessage, OpenAIMessageToolResponse, OpenAIContents, OpenAIText, OpenAIImageURL, OpenAIImageURLURL, OpenAIToolCall, OpenAIToolCallFunction
2
2
 
3
3
  __all__ = [
4
4
  "to_openai",
@@ -6,9 +6,9 @@ __all__ = [
6
6
  "OpenAIMessage",
7
7
  "OpenAIMessageToolResponse",
8
8
  "OpenAIContents",
9
- "OpenAITextContent",
10
- "OpenAIImageURLContent",
9
+ "OpenAIText",
10
+ "OpenAIImageURL",
11
11
  "OpenAIImageURLURL",
12
- "OpenAIToolCallContent",
12
+ "OpenAIToolCall",
13
13
  "OpenAIToolCallFunction",
14
14
  ]
@@ -1,19 +1,19 @@
1
- from typing import TypedDict, Literal
1
+ from typing import TypedDict, Literal, Required
2
2
 
3
3
  from ..messages import AIMessages, AIMessageToolResponse
4
- from ..chunks import AIChunks, AIChunkText, AIChunkImageURL, AIChunkFile, AIChunkToolCall
4
+ from ..chunks import AIChunkText, AIChunkImageURL, AIChunkFile, AIChunkToolCall
5
5
  import base64
6
6
  import json
7
7
 
8
8
 
9
- class OpenAITextContent(TypedDict):
9
+ class OpenAIText(TypedDict):
10
10
  type: Literal["text"]
11
11
  text: str
12
12
 
13
13
  class OpenAIImageURLURL(TypedDict):
14
14
  url: str
15
15
 
16
- class OpenAIImageURLContent(TypedDict):
16
+ class OpenAIImageURL(TypedDict):
17
17
  type: Literal["image_url"]
18
18
  image_url: OpenAIImageURLURL
19
19
 
@@ -22,22 +22,23 @@ class OpenAIToolCallFunction(TypedDict):
22
22
  name: str
23
23
  arguments: str
24
24
 
25
- class OpenAIToolCallContent(TypedDict):
25
+ class OpenAIToolCall(TypedDict):
26
26
  id: str
27
27
  type: Literal["function"]
28
28
  function: OpenAIToolCallFunction
29
29
 
30
30
 
31
31
 
32
- OpenAIContents = OpenAITextContent | OpenAIImageURLContent | OpenAIToolCallContent
32
+ OpenAIContents = OpenAIText | OpenAIImageURL
33
33
 
34
34
 
35
- class OpenAIMessage(TypedDict):
36
- role: str
35
+ class OpenAIMessage(TypedDict, total=False):
36
+ role: Required[str]
37
37
  content: list[OpenAIContents]
38
+ tool_calls: list[OpenAIToolCall]
38
39
 
39
- class OpenAIMessageToolResponse(TypedDict):
40
- role: Literal["tool"]
40
+ class OpenAIMessageToolResponse(TypedDict, total=False):
41
+ role: Required[Literal["tool"]]
41
42
  tool_call_id: str
42
43
  content: str
43
44
 
@@ -75,30 +76,40 @@ def to_openai(messages: list[AIMessages]) -> list[OpenAIMessages]:
75
76
  OpenAIMessage(
76
77
  role="user", # Hacky, but what else to circumvent API limitations in a broadly compatible way?
77
78
  content=[
78
- chunk_to_openai(chunk) for chunk in media_chunks
79
- ]
79
+ content_chunk_to_openai(chunk) for chunk in media_chunks
80
+ ],
80
81
  )
81
82
  )
82
83
  else:
83
- result.append(OpenAIMessage(
84
- role= role,
85
- content= [
86
- chunk_to_openai(chunk) for chunk in message.chunks
87
- ]
88
- ))
84
+ formatted = OpenAIMessage(
85
+ role=role
86
+ )
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
+
89
100
  return result
90
101
 
91
102
 
92
- def chunk_to_openai(chunk: AIChunks) -> OpenAIContents:
103
+ def content_chunk_to_openai(chunk: AIChunkText | AIChunkFile | AIChunkImageURL) -> OpenAIContents:
93
104
 
94
105
  match chunk:
95
106
  case AIChunkText():
96
- return OpenAITextContent(
107
+ return OpenAIText(
97
108
  type="text",
98
109
  text=chunk.text,
99
110
  )
100
111
  case AIChunkImageURL():
101
- return OpenAIImageURLContent(
112
+ return OpenAIImageURL(
102
113
  type="image_url",
103
114
  image_url={
104
115
  "url": chunk.url,
@@ -107,7 +118,7 @@ def chunk_to_openai(chunk: AIChunks) -> OpenAIContents:
107
118
  case AIChunkFile():
108
119
  if chunk.mimetype.startswith("image/"):
109
120
  base64_data = base64.b64encode(chunk.bytes).decode('utf-8')
110
- return OpenAIImageURLContent(
121
+ return OpenAIImageURL(
111
122
  type= "image_url",
112
123
  image_url= {
113
124
  "url": f"data:{chunk.mimetype};base64,{base64_data}",
@@ -115,20 +126,22 @@ def chunk_to_openai(chunk: AIChunks) -> OpenAIContents:
115
126
  )
116
127
  elif chunk.mimetype == "text/plain":
117
128
  text = chunk.bytes.decode(encoding="utf-8")
118
- return OpenAITextContent(
129
+ return OpenAIText(
119
130
  type="text",
120
131
  text=text
121
132
  )
122
133
  else:
123
134
  raise ValueError(f"Unsupported file type for OpenAI: {chunk.mimetype}")
124
- case AIChunkToolCall():
125
- return OpenAIToolCallContent(
135
+ case _:
136
+ raise ValueError(f"Unsupported chunk type: {type(chunk)}")
137
+
138
+ def tool_call_chunk_to_openai(chunk: AIChunkToolCall) -> OpenAIToolCall:
139
+
140
+ return OpenAIToolCall(
126
141
  id=chunk.id,
127
142
  type="function",
128
143
  function=OpenAIToolCallFunction(
129
144
  name=chunk.name,
130
145
  arguments=json.dumps(chunk.arguments)
131
146
  )
132
- )
133
- case _:
134
- raise ValueError(f"Unsupported chunk type: {type(chunk)}")
147
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: llmir
3
- Version: 0.0.8
3
+ Version: 0.0.10
4
4
  Summary: Core message and tool IR for LLM pipelines
5
5
  Author: Mathis Siebert
6
6
  Requires-Python: >=3.11
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "llmir"
3
- version = "0.0.8"
3
+ version = "0.0.10"
4
4
  description = "Core message and tool IR for LLM pipelines"
5
5
  authors = [{ name="Mathis Siebert" }]
6
6
  requires-python = ">=3.11"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes