ommlds 0.0.0.dev476__py3-none-any.whl → 0.0.0.dev478__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.
- ommlds/.omlish-manifests.json +18 -6
- ommlds/backends/groq/protocol.py +69 -4
- ommlds/cli/sessions/chat/chat/ai/rendering.py +3 -3
- ommlds/cli/sessions/chat/chat/ai/services.py +2 -2
- ommlds/cli/sessions/chat/chat/ai/types.py +1 -1
- ommlds/cli/sessions/chat/tools/configs.py +9 -0
- ommlds/cli/sessions/chat/tools/fs/__init__.py +0 -0
- ommlds/cli/sessions/chat/tools/fs/configs.py +12 -0
- ommlds/cli/sessions/chat/tools/fs/inject.py +35 -0
- ommlds/cli/sessions/chat/tools/inject.py +11 -64
- ommlds/cli/sessions/chat/tools/injection.py +14 -0
- ommlds/cli/sessions/chat/tools/todo/__init__.py +0 -0
- ommlds/cli/sessions/chat/tools/todo/configs.py +12 -0
- ommlds/cli/sessions/chat/tools/todo/inject.py +31 -0
- ommlds/cli/sessions/chat/tools/weather/__init__.py +0 -0
- ommlds/cli/sessions/chat/tools/weather/configs.py +12 -0
- ommlds/cli/sessions/chat/tools/weather/inject.py +22 -0
- ommlds/cli/sessions/chat/tools/{weather.py → weather/tools.py} +1 -1
- ommlds/minichain/__init__.py +39 -22
- ommlds/minichain/backends/impls/anthropic/stream.py +11 -11
- ommlds/minichain/backends/impls/dummy/chat.py +7 -7
- ommlds/minichain/backends/impls/google/stream.py +9 -9
- ommlds/minichain/backends/impls/groq/chat.py +11 -5
- ommlds/minichain/backends/impls/groq/names.py +13 -0
- ommlds/minichain/backends/impls/groq/protocol.py +120 -23
- ommlds/minichain/backends/impls/groq/stream.py +14 -10
- ommlds/minichain/backends/impls/llamacpp/stream.py +7 -7
- ommlds/minichain/backends/impls/mlx/chat.py +7 -7
- ommlds/minichain/backends/impls/ollama/chat.py +7 -7
- ommlds/minichain/backends/impls/openai/format.py +10 -10
- ommlds/minichain/backends/impls/openai/names.py +2 -2
- ommlds/minichain/backends/impls/openai/stream.py +8 -8
- ommlds/minichain/backends/impls/tinygrad/chat.py +7 -7
- ommlds/minichain/backends/impls/transformers/transformers.py +7 -7
- ommlds/minichain/chat/choices/stream/__init__.py +0 -0
- ommlds/minichain/chat/{stream → choices/stream}/adapters.py +7 -7
- ommlds/minichain/chat/choices/stream/joining.py +31 -0
- ommlds/minichain/chat/choices/stream/services.py +45 -0
- ommlds/minichain/chat/choices/stream/types.py +43 -0
- ommlds/minichain/chat/stream/_marshal.py +4 -4
- ommlds/minichain/chat/stream/joining.py +32 -43
- ommlds/minichain/chat/stream/services.py +15 -15
- ommlds/minichain/chat/stream/types.py +13 -23
- ommlds/minichain/tools/reflect.py +5 -1
- {ommlds-0.0.0.dev476.dist-info → ommlds-0.0.0.dev478.dist-info}/METADATA +3 -3
- {ommlds-0.0.0.dev476.dist-info → ommlds-0.0.0.dev478.dist-info}/RECORD +50 -37
- {ommlds-0.0.0.dev476.dist-info → ommlds-0.0.0.dev478.dist-info}/WHEEL +0 -0
- {ommlds-0.0.0.dev476.dist-info → ommlds-0.0.0.dev478.dist-info}/entry_points.txt +0 -0
- {ommlds-0.0.0.dev476.dist-info → ommlds-0.0.0.dev478.dist-info}/licenses/LICENSE +0 -0
- {ommlds-0.0.0.dev476.dist-info → ommlds-0.0.0.dev478.dist-info}/top_level.txt +0 -0
|
@@ -11,6 +11,11 @@ from omlish.http import all as http
|
|
|
11
11
|
from omlish.io.buffers import DelimitingBuffer
|
|
12
12
|
|
|
13
13
|
from .....backends.google.protocol import types as pt
|
|
14
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
15
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
16
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
17
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
18
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
14
19
|
from ....chat.choices.types import ChatChoicesOutputs
|
|
15
20
|
from ....chat.messages import AiMessage
|
|
16
21
|
from ....chat.messages import Message
|
|
@@ -18,13 +23,8 @@ from ....chat.messages import SystemMessage
|
|
|
18
23
|
from ....chat.messages import ToolUseMessage
|
|
19
24
|
from ....chat.messages import ToolUseResultMessage
|
|
20
25
|
from ....chat.messages import UserMessage
|
|
21
|
-
from ....chat.stream.
|
|
22
|
-
from ....chat.stream.
|
|
23
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
24
|
-
from ....chat.stream.types import AiChoiceDeltas
|
|
25
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
26
|
-
from ....chat.stream.types import ContentAiChoiceDelta
|
|
27
|
-
from ....chat.stream.types import ToolUseAiChoiceDelta
|
|
26
|
+
from ....chat.stream.types import ContentAiDelta
|
|
27
|
+
from ....chat.stream.types import ToolUseAiDelta
|
|
28
28
|
from ....chat.tools.types import Tool
|
|
29
29
|
from ....models.configs import ModelName
|
|
30
30
|
from ....resources import UseResources
|
|
@@ -194,7 +194,7 @@ class GoogleChatChoicesStreamService:
|
|
|
194
194
|
check.none(p.function_call)
|
|
195
195
|
await sink.emit(AiChoicesDeltas([
|
|
196
196
|
AiChoiceDeltas([
|
|
197
|
-
|
|
197
|
+
ContentAiDelta(check.not_none(txt)),
|
|
198
198
|
]),
|
|
199
199
|
]))
|
|
200
200
|
|
|
@@ -202,7 +202,7 @@ class GoogleChatChoicesStreamService:
|
|
|
202
202
|
check.none(p.text)
|
|
203
203
|
await sink.emit(AiChoicesDeltas([
|
|
204
204
|
AiChoiceDeltas([
|
|
205
|
-
|
|
205
|
+
ToolUseAiDelta(
|
|
206
206
|
id=fc.id,
|
|
207
207
|
name=fc.name,
|
|
208
208
|
args=fc.args,
|
|
@@ -10,11 +10,13 @@ from .....backends.groq import protocol as pt
|
|
|
10
10
|
from ....chat.choices.services import ChatChoicesRequest
|
|
11
11
|
from ....chat.choices.services import ChatChoicesResponse
|
|
12
12
|
from ....chat.choices.services import static_check_is_chat_choices_service
|
|
13
|
+
from ....chat.tools.types import Tool
|
|
13
14
|
from ....models.configs import ModelName
|
|
14
15
|
from ....standard import ApiKey
|
|
15
16
|
from ....standard import DefaultOptions
|
|
16
17
|
from .names import MODEL_NAMES
|
|
17
|
-
from .protocol import
|
|
18
|
+
from .protocol import build_gq_request_messages
|
|
19
|
+
from .protocol import build_gq_request_tool
|
|
18
20
|
from .protocol import build_mc_choices_response
|
|
19
21
|
|
|
20
22
|
|
|
@@ -44,12 +46,16 @@ class GroqChatChoicesService:
|
|
|
44
46
|
self._default_options: tv.TypedValues = DefaultOptions.pop(cc)
|
|
45
47
|
|
|
46
48
|
async def invoke(self, request: ChatChoicesRequest) -> ChatChoicesResponse:
|
|
49
|
+
tools: list[pt.ChatCompletionRequest.Tool] = []
|
|
50
|
+
with tv.TypedValues(*request.options).consume() as oc:
|
|
51
|
+
t: Tool
|
|
52
|
+
for t in oc.pop(Tool, []):
|
|
53
|
+
tools.append(build_gq_request_tool(t))
|
|
54
|
+
|
|
47
55
|
gq_request = pt.ChatCompletionRequest(
|
|
48
|
-
messages=
|
|
49
|
-
build_gq_request_message(m)
|
|
50
|
-
for m in request.v
|
|
51
|
-
],
|
|
56
|
+
messages=build_gq_request_messages(request.v),
|
|
52
57
|
model=MODEL_NAMES.resolve(self._model_name.v),
|
|
58
|
+
tools=tools or None,
|
|
53
59
|
)
|
|
54
60
|
|
|
55
61
|
raw_request = msh.marshal(gq_request)
|
|
@@ -4,6 +4,19 @@ https://console.groq.com/docs/models
|
|
|
4
4
|
curl -X GET "https://api.groq.com/openai/v1/models" \
|
|
5
5
|
-H "Authorization: Bearer $GROQ_API_KEY" \
|
|
6
6
|
-H "Content-Type: application/json"
|
|
7
|
+
|
|
8
|
+
"compound-beta",
|
|
9
|
+
"compound-beta-mini",
|
|
10
|
+
"gemma2-9b-it",
|
|
11
|
+
"llama-3.1-8b-instant",
|
|
12
|
+
"llama-3.3-70b-versatile",
|
|
13
|
+
"meta-llama/llama-4-maverick-17b-128e-instruct",
|
|
14
|
+
"meta-llama/llama-4-scout-17b-16e-instruct",
|
|
15
|
+
"meta-llama/llama-guard-4-12b",
|
|
16
|
+
"moonshotai/kimi-k2-instruct",
|
|
17
|
+
"openai/gpt-oss-120b",
|
|
18
|
+
"openai/gpt-oss-20b",
|
|
19
|
+
"qwen/qwen3-32b",
|
|
7
20
|
"""
|
|
8
21
|
from ....models.names import ModelNameCollection
|
|
9
22
|
from ...strings.manifests import BackendStringsManifest
|
|
@@ -1,46 +1,143 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
1
3
|
from omlish import check
|
|
4
|
+
from omlish.formats import json
|
|
2
5
|
|
|
3
6
|
from .....backends.groq import protocol as pt
|
|
4
7
|
from ....chat.choices.services import ChatChoicesResponse
|
|
8
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
5
9
|
from ....chat.choices.types import AiChoice
|
|
6
10
|
from ....chat.messages import AiMessage
|
|
7
|
-
from ....chat.messages import
|
|
11
|
+
from ....chat.messages import AnyAiMessage
|
|
12
|
+
from ....chat.messages import Chat
|
|
8
13
|
from ....chat.messages import SystemMessage
|
|
14
|
+
from ....chat.messages import ToolUseMessage
|
|
15
|
+
from ....chat.messages import ToolUseResultMessage
|
|
9
16
|
from ....chat.messages import UserMessage
|
|
10
|
-
from ....chat.stream.types import
|
|
11
|
-
from ....chat.stream.types import
|
|
17
|
+
from ....chat.stream.types import AiDelta
|
|
18
|
+
from ....chat.stream.types import ContentAiDelta
|
|
19
|
+
from ....chat.stream.types import ToolUseAiDelta
|
|
20
|
+
from ....chat.tools.types import Tool
|
|
21
|
+
from ....content.prepare import prepare_content_str
|
|
22
|
+
from ....tools.jsonschema import build_tool_spec_params_json_schema
|
|
23
|
+
from ....tools.types import ToolUse
|
|
12
24
|
|
|
13
25
|
|
|
14
26
|
##
|
|
15
27
|
|
|
16
28
|
|
|
17
|
-
def
|
|
18
|
-
|
|
19
|
-
return pt.ChatCompletionRequest.SystemMessage(
|
|
20
|
-
content=check.isinstance(mc_msg.c, str),
|
|
21
|
-
)
|
|
29
|
+
def build_gq_request_messages(chat: Chat) -> list[pt.ChatCompletionRequest.Message]:
|
|
30
|
+
gq_msgs: list[pt.ChatCompletionRequest.Message] = []
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
content=check.isinstance(mc_msg.c, str),
|
|
26
|
-
)
|
|
32
|
+
for _, g in itertools.groupby(chat, lambda mc_m: isinstance(mc_m, AnyAiMessage)):
|
|
33
|
+
mc_msgs = list(g)
|
|
27
34
|
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
if isinstance(mc_msgs[0], AnyAiMessage):
|
|
36
|
+
tups: list[tuple[AiMessage | None, list[ToolUseMessage]]] = []
|
|
37
|
+
for mc_msg in mc_msgs:
|
|
38
|
+
if isinstance(mc_msg, AiMessage):
|
|
39
|
+
tups.append((mc_msg, []))
|
|
40
|
+
|
|
41
|
+
elif isinstance(mc_msg, ToolUseMessage):
|
|
42
|
+
if not tups:
|
|
43
|
+
tups.append((None, []))
|
|
44
|
+
tups[-1][1].append(mc_msg)
|
|
45
|
+
|
|
46
|
+
else:
|
|
47
|
+
raise TypeError(mc_msg)
|
|
48
|
+
|
|
49
|
+
for mc_ai_msg, mc_tu_msgs in tups:
|
|
50
|
+
gq_msgs.append(pt.ChatCompletionRequest.AssistantMessage(
|
|
51
|
+
content=check.isinstance(mc_ai_msg.c, str) if mc_ai_msg is not None else None,
|
|
52
|
+
tool_calls=[
|
|
53
|
+
pt.ChatCompletionRequest.AssistantMessage.ToolCall(
|
|
54
|
+
function=pt.ChatCompletionRequest.AssistantMessage.ToolCall.Function(
|
|
55
|
+
name=mc_tu_msg.tu.name,
|
|
56
|
+
arguments=check.not_none(mc_tu_msg.tu.raw_args),
|
|
57
|
+
),
|
|
58
|
+
id=check.not_none(mc_tu_msg.tu.id),
|
|
59
|
+
)
|
|
60
|
+
for mc_tu_msg in mc_tu_msgs
|
|
61
|
+
] if mc_tu_msgs else None,
|
|
62
|
+
))
|
|
63
|
+
|
|
64
|
+
else:
|
|
65
|
+
for mc_msg in mc_msgs:
|
|
66
|
+
if isinstance(mc_msg, SystemMessage):
|
|
67
|
+
gq_msgs.append(pt.ChatCompletionRequest.SystemMessage(
|
|
68
|
+
content=check.isinstance(mc_msg.c, str),
|
|
69
|
+
))
|
|
70
|
+
|
|
71
|
+
elif isinstance(mc_msg, UserMessage):
|
|
72
|
+
gq_msgs.append(pt.ChatCompletionRequest.UserMessage(
|
|
73
|
+
content=check.isinstance(mc_msg.c, str),
|
|
74
|
+
))
|
|
75
|
+
|
|
76
|
+
elif isinstance(mc_msg, ToolUseResultMessage):
|
|
77
|
+
gq_msgs.append(pt.ChatCompletionRequest.ToolMessage(
|
|
78
|
+
tool_call_id=check.not_none(mc_msg.tur.id),
|
|
79
|
+
content=check.isinstance(mc_msg.tur.c, str),
|
|
80
|
+
))
|
|
81
|
+
|
|
82
|
+
else:
|
|
83
|
+
raise TypeError(mc_msg)
|
|
84
|
+
|
|
85
|
+
return gq_msgs
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def build_gq_request_tool(t: Tool) -> pt.ChatCompletionRequest.Tool:
|
|
89
|
+
return pt.ChatCompletionRequest.Tool(
|
|
90
|
+
function=pt.ChatCompletionRequest.Tool.Function(
|
|
91
|
+
name=check.not_none(t.spec.name),
|
|
92
|
+
description=prepare_content_str(t.spec.desc),
|
|
93
|
+
parameters=build_tool_spec_params_json_schema(t.spec),
|
|
94
|
+
),
|
|
95
|
+
)
|
|
30
96
|
|
|
31
97
|
|
|
32
98
|
def build_mc_choices_response(gq_resp: pt.ChatCompletionResponse) -> ChatChoicesResponse:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
99
|
+
def build_choice(gq_choice: pt.ChatCompletionResponse.Choice) -> AiChoice:
|
|
100
|
+
gq_msg = gq_choice.message
|
|
101
|
+
|
|
102
|
+
lst: list[AnyAiMessage] = []
|
|
103
|
+
|
|
104
|
+
if gq_msg.content is not None:
|
|
105
|
+
lst.append(AiMessage(
|
|
106
|
+
check.isinstance(gq_msg.content, str),
|
|
107
|
+
))
|
|
108
|
+
|
|
109
|
+
for gq_tc in gq_msg.tool_calls or []:
|
|
110
|
+
lst.append(ToolUseMessage(ToolUse(
|
|
111
|
+
id=gq_tc.id,
|
|
112
|
+
name=gq_tc.function.name,
|
|
113
|
+
args=json.loads(gq_tc.function.arguments or '{}'),
|
|
114
|
+
raw_args=gq_tc.function.arguments,
|
|
115
|
+
)))
|
|
116
|
+
|
|
117
|
+
return AiChoice(lst)
|
|
118
|
+
|
|
119
|
+
return ChatChoicesResponse(list(map(build_choice, gq_resp.choices)))
|
|
39
120
|
|
|
40
121
|
|
|
41
122
|
def build_mc_ai_choice_deltas(delta: pt.ChatCompletionChunk.Choice.Delta) -> AiChoiceDeltas:
|
|
42
|
-
if delta.role in (None, 'assistant')
|
|
43
|
-
|
|
123
|
+
if delta.role in (None, 'assistant'):
|
|
124
|
+
lst: list[AiDelta] = []
|
|
44
125
|
|
|
45
|
-
|
|
126
|
+
if delta.content is not None:
|
|
127
|
+
lst.append(ContentAiDelta(delta.content))
|
|
128
|
+
|
|
129
|
+
for tc in delta.tool_calls or []:
|
|
130
|
+
tc_fn = check.not_none(tc.function)
|
|
131
|
+
lst.append(ToolUseAiDelta(
|
|
132
|
+
id=tc.id,
|
|
133
|
+
name=check.not_none(tc_fn.name),
|
|
134
|
+
args=json.loads(tc_fn.arguments or '{}'),
|
|
135
|
+
))
|
|
136
|
+
|
|
137
|
+
return AiChoiceDeltas(lst)
|
|
138
|
+
|
|
139
|
+
elif delta.channel in ('analysis', 'commentary'):
|
|
46
140
|
return AiChoiceDeltas([])
|
|
141
|
+
|
|
142
|
+
else:
|
|
143
|
+
raise ValueError(delta)
|
|
@@ -10,10 +10,11 @@ from omlish.io.buffers import DelimitingBuffer
|
|
|
10
10
|
|
|
11
11
|
from .....backends.groq import protocol as pt
|
|
12
12
|
from ....chat.choices.services import ChatChoicesOutputs
|
|
13
|
-
from ....chat.stream.services import ChatChoicesStreamRequest
|
|
14
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
15
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
16
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
13
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
14
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
15
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
16
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
17
|
+
from ....chat.tools.types import Tool
|
|
17
18
|
from ....configs import Config
|
|
18
19
|
from ....resources import UseResources
|
|
19
20
|
from ....standard import ApiKey
|
|
@@ -21,7 +22,8 @@ from ....stream.services import StreamResponseSink
|
|
|
21
22
|
from ....stream.services import new_stream_response
|
|
22
23
|
from .chat import GroqChatChoicesService
|
|
23
24
|
from .names import MODEL_NAMES
|
|
24
|
-
from .protocol import
|
|
25
|
+
from .protocol import build_gq_request_messages
|
|
26
|
+
from .protocol import build_gq_request_tool
|
|
25
27
|
from .protocol import build_mc_ai_choice_deltas
|
|
26
28
|
|
|
27
29
|
|
|
@@ -50,14 +52,16 @@ class GroqChatChoicesStreamService:
|
|
|
50
52
|
READ_CHUNK_SIZE: ta.ClassVar[int] = -1
|
|
51
53
|
|
|
52
54
|
async def invoke(self, request: ChatChoicesStreamRequest) -> ChatChoicesStreamResponse:
|
|
53
|
-
|
|
55
|
+
tools: list[pt.ChatCompletionRequest.Tool] = []
|
|
56
|
+
with tv.TypedValues(*request.options).consume() as oc:
|
|
57
|
+
t: Tool
|
|
58
|
+
for t in oc.pop(Tool, []):
|
|
59
|
+
tools.append(build_gq_request_tool(t))
|
|
54
60
|
|
|
55
61
|
gq_request = pt.ChatCompletionRequest(
|
|
56
|
-
messages=
|
|
57
|
-
build_gq_request_message(m)
|
|
58
|
-
for m in request.v
|
|
59
|
-
],
|
|
62
|
+
messages=build_gq_request_messages(request.v),
|
|
60
63
|
model=MODEL_NAMES.resolve(self._model_name.v),
|
|
64
|
+
tools=tools or None,
|
|
61
65
|
stream=True,
|
|
62
66
|
)
|
|
63
67
|
|
|
@@ -10,12 +10,12 @@ from omlish import typedvalues as tv
|
|
|
10
10
|
|
|
11
11
|
from .....backends import llamacpp as lcu
|
|
12
12
|
from ....chat.choices.services import ChatChoicesOutputs
|
|
13
|
-
from ....chat.stream.services import ChatChoicesStreamRequest
|
|
14
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
15
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
16
|
-
from ....chat.stream.types import AiChoiceDeltas
|
|
17
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
18
|
-
from ....chat.stream.types import
|
|
13
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
14
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
15
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
16
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
17
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
18
|
+
from ....chat.stream.types import ContentAiDelta
|
|
19
19
|
from ....configs import Config
|
|
20
20
|
from ....models.configs import ModelPath
|
|
21
21
|
from ....resources import UseResources
|
|
@@ -102,7 +102,7 @@ class LlamacppChatChoicesStreamService(lang.ExitStacked):
|
|
|
102
102
|
# FIXME: stop reason
|
|
103
103
|
|
|
104
104
|
if (content := delta.get('content', '')):
|
|
105
|
-
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([
|
|
105
|
+
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([ContentAiDelta(content)])]))
|
|
106
106
|
|
|
107
107
|
return None
|
|
108
108
|
|
|
@@ -10,18 +10,18 @@ from ....chat.choices.services import ChatChoicesOutputs
|
|
|
10
10
|
from ....chat.choices.services import ChatChoicesRequest
|
|
11
11
|
from ....chat.choices.services import ChatChoicesResponse
|
|
12
12
|
from ....chat.choices.services import static_check_is_chat_choices_service
|
|
13
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
14
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
15
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
16
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
17
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
13
18
|
from ....chat.choices.types import AiChoice
|
|
14
19
|
from ....chat.choices.types import ChatChoicesOptions
|
|
15
20
|
from ....chat.messages import AiMessage
|
|
16
21
|
from ....chat.messages import Message
|
|
17
22
|
from ....chat.messages import SystemMessage
|
|
18
23
|
from ....chat.messages import UserMessage
|
|
19
|
-
from ....chat.stream.
|
|
20
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
21
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
22
|
-
from ....chat.stream.types import AiChoiceDeltas
|
|
23
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
24
|
-
from ....chat.stream.types import ContentAiChoiceDelta
|
|
24
|
+
from ....chat.stream.types import ContentAiDelta
|
|
25
25
|
from ....configs import Config
|
|
26
26
|
from ....llms.types import MaxTokens
|
|
27
27
|
from ....models.configs import ModelPath
|
|
@@ -207,7 +207,7 @@ class MlxChatChoicesStreamService(BaseMlxChatChoicesService):
|
|
|
207
207
|
for go in gen:
|
|
208
208
|
if go.text:
|
|
209
209
|
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([
|
|
210
|
-
|
|
210
|
+
ContentAiDelta(go.text),
|
|
211
211
|
])]))
|
|
212
212
|
|
|
213
213
|
return []
|
|
@@ -13,18 +13,18 @@ from ....chat.choices.services import ChatChoicesOutputs
|
|
|
13
13
|
from ....chat.choices.services import ChatChoicesRequest
|
|
14
14
|
from ....chat.choices.services import ChatChoicesResponse
|
|
15
15
|
from ....chat.choices.services import static_check_is_chat_choices_service
|
|
16
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
17
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
18
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
19
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
20
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
16
21
|
from ....chat.choices.types import AiChoice
|
|
17
22
|
from ....chat.messages import AiMessage
|
|
18
23
|
from ....chat.messages import AnyAiMessage
|
|
19
24
|
from ....chat.messages import Message
|
|
20
25
|
from ....chat.messages import SystemMessage
|
|
21
26
|
from ....chat.messages import UserMessage
|
|
22
|
-
from ....chat.stream.
|
|
23
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
24
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
25
|
-
from ....chat.stream.types import AiChoiceDeltas
|
|
26
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
27
|
-
from ....chat.stream.types import ContentAiChoiceDelta
|
|
27
|
+
from ....chat.stream.types import ContentAiDelta
|
|
28
28
|
from ....models.configs import ModelName
|
|
29
29
|
from ....resources import UseResources
|
|
30
30
|
from ....standard import ApiUrl
|
|
@@ -189,7 +189,7 @@ class OllamaChatChoicesStreamService(BaseOllamaChatChoicesService):
|
|
|
189
189
|
check.state(not lp.message.tool_calls)
|
|
190
190
|
|
|
191
191
|
if (c := lp.message.content):
|
|
192
|
-
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([
|
|
192
|
+
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([ContentAiDelta(
|
|
193
193
|
c,
|
|
194
194
|
)])]))
|
|
195
195
|
|
|
@@ -17,9 +17,9 @@ from ....chat.messages import SystemMessage
|
|
|
17
17
|
from ....chat.messages import ToolUseMessage
|
|
18
18
|
from ....chat.messages import ToolUseResultMessage
|
|
19
19
|
from ....chat.messages import UserMessage
|
|
20
|
-
from ....chat.stream.types import
|
|
21
|
-
from ....chat.stream.types import
|
|
22
|
-
from ....chat.stream.types import
|
|
20
|
+
from ....chat.stream.types import AiDelta
|
|
21
|
+
from ....chat.stream.types import ContentAiDelta
|
|
22
|
+
from ....chat.stream.types import PartialToolUseAiDelta
|
|
23
23
|
from ....chat.tools.types import Tool
|
|
24
24
|
from ....content.json import JsonContent
|
|
25
25
|
from ....content.prepare import prepare_content_str
|
|
@@ -128,16 +128,16 @@ def build_mc_choices_response(oai_resp: pt.ChatCompletionResponse) -> ChatChoice
|
|
|
128
128
|
)
|
|
129
129
|
|
|
130
130
|
|
|
131
|
-
def
|
|
131
|
+
def build_mc_ai_delta(delta: pt.ChatCompletionChunkChoiceDelta) -> AiDelta:
|
|
132
132
|
if delta.content is not None:
|
|
133
133
|
check.state(not delta.tool_calls)
|
|
134
|
-
return
|
|
134
|
+
return ContentAiDelta(delta.content)
|
|
135
135
|
|
|
136
136
|
elif delta.tool_calls is not None:
|
|
137
137
|
check.state(delta.content is None)
|
|
138
138
|
tc = check.single(delta.tool_calls)
|
|
139
139
|
tc_fn = check.not_none(tc.function)
|
|
140
|
-
return
|
|
140
|
+
return PartialToolUseAiDelta(
|
|
141
141
|
id=tc.id,
|
|
142
142
|
name=tc_fn.name,
|
|
143
143
|
raw_args=tc_fn.arguments,
|
|
@@ -166,8 +166,8 @@ class OpenaiChatRequestHandler:
|
|
|
166
166
|
self._mandatory_kwargs = mandatory_kwargs
|
|
167
167
|
|
|
168
168
|
DEFAULT_OPTIONS: ta.ClassVar[tv.TypedValues[Option]] = tv.TypedValues[Option](
|
|
169
|
-
Temperature(0.),
|
|
170
|
-
MaxTokens(1024),
|
|
169
|
+
# Temperature(0.),
|
|
170
|
+
# MaxTokens(1024),
|
|
171
171
|
)
|
|
172
172
|
|
|
173
173
|
_OPTION_KWARG_NAMES_MAP: ta.ClassVar[ta.Mapping[str, type[ChatChoicesOptions]]] = dict(
|
|
@@ -182,8 +182,8 @@ class OpenaiChatRequestHandler:
|
|
|
182
182
|
@cached.function
|
|
183
183
|
def _process_options(self) -> _ProcessedOptions:
|
|
184
184
|
kwargs: dict = dict(
|
|
185
|
-
temperature=0,
|
|
186
|
-
max_tokens=1024,
|
|
185
|
+
# temperature=0,
|
|
186
|
+
# max_tokens=1024,
|
|
187
187
|
)
|
|
188
188
|
|
|
189
189
|
tools_by_name: dict[str, ToolSpec] = {}
|
|
@@ -13,12 +13,12 @@ from omlish.io.buffers import DelimitingBuffer
|
|
|
13
13
|
|
|
14
14
|
from .....backends.openai import protocol as pt
|
|
15
15
|
from ....chat.choices.services import ChatChoicesOutputs
|
|
16
|
-
from ....chat.stream.services import ChatChoicesStreamRequest
|
|
17
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
18
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
19
|
-
from ....chat.stream.types import AiChoiceDeltas
|
|
20
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
21
|
-
from ....chat.stream.types import ChatChoicesStreamOption
|
|
16
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
17
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
18
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
19
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
20
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
21
|
+
from ....chat.choices.stream.types import ChatChoicesStreamOption
|
|
22
22
|
from ....configs import Config
|
|
23
23
|
from ....resources import ResourcesOption
|
|
24
24
|
from ....resources import UseResources
|
|
@@ -28,7 +28,7 @@ from ....stream.services import StreamResponseSink
|
|
|
28
28
|
from ....stream.services import new_stream_response
|
|
29
29
|
from .chat import OpenaiChatChoicesService
|
|
30
30
|
from .format import OpenaiChatRequestHandler
|
|
31
|
-
from .format import
|
|
31
|
+
from .format import build_mc_ai_delta
|
|
32
32
|
from .names import CHAT_MODEL_NAMES
|
|
33
33
|
|
|
34
34
|
|
|
@@ -122,7 +122,7 @@ class OpenaiChatChoicesStreamService:
|
|
|
122
122
|
break
|
|
123
123
|
|
|
124
124
|
await sink.emit(AiChoicesDeltas([
|
|
125
|
-
AiChoiceDeltas([
|
|
125
|
+
AiChoiceDeltas([build_mc_ai_delta(choice.delta)])
|
|
126
126
|
for choice in ccc.choices
|
|
127
127
|
]))
|
|
128
128
|
|
|
@@ -9,18 +9,18 @@ from ....chat.choices.services import ChatChoicesOptions
|
|
|
9
9
|
from ....chat.choices.services import ChatChoicesRequest
|
|
10
10
|
from ....chat.choices.services import ChatChoicesResponse
|
|
11
11
|
from ....chat.choices.services import static_check_is_chat_choices_service
|
|
12
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
13
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
14
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
15
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
16
|
+
from ....chat.choices.stream.types import AiChoicesDeltas
|
|
12
17
|
from ....chat.choices.types import AiChoice
|
|
13
18
|
from ....chat.choices.types import ChatChoicesOutputs
|
|
14
19
|
from ....chat.messages import AiMessage
|
|
15
20
|
from ....chat.messages import Chat
|
|
16
21
|
from ....chat.messages import SystemMessage
|
|
17
22
|
from ....chat.messages import UserMessage
|
|
18
|
-
from ....chat.stream.
|
|
19
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
20
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
21
|
-
from ....chat.stream.types import AiChoiceDeltas
|
|
22
|
-
from ....chat.stream.types import AiChoicesDeltas
|
|
23
|
-
from ....chat.stream.types import ContentAiChoiceDelta
|
|
23
|
+
from ....chat.stream.types import ContentAiDelta
|
|
24
24
|
from ....chat.types import ChatOption
|
|
25
25
|
from ....llms.types import LlmOption
|
|
26
26
|
from ....resources import UseResources
|
|
@@ -151,7 +151,7 @@ class TinygradLlama3ChatChoicesStreamService(BaseTinygradLlama3ChatService):
|
|
|
151
151
|
for s in tgl3.run_llm(llm, toks):
|
|
152
152
|
await sink.emit(AiChoicesDeltas([
|
|
153
153
|
AiChoiceDeltas([
|
|
154
|
-
|
|
154
|
+
ContentAiDelta(s),
|
|
155
155
|
]),
|
|
156
156
|
]))
|
|
157
157
|
|
|
@@ -19,6 +19,11 @@ from .....backends.transformers.streamers import CancellableTextStreamer
|
|
|
19
19
|
from ....chat.choices.services import ChatChoicesRequest
|
|
20
20
|
from ....chat.choices.services import ChatChoicesResponse
|
|
21
21
|
from ....chat.choices.services import static_check_is_chat_choices_service
|
|
22
|
+
from ....chat.choices.stream.services import ChatChoicesStreamRequest
|
|
23
|
+
from ....chat.choices.stream.services import ChatChoicesStreamResponse
|
|
24
|
+
from ....chat.choices.stream.services import static_check_is_chat_choices_stream_service
|
|
25
|
+
from ....chat.choices.stream.types import AiChoiceDeltas # noqa
|
|
26
|
+
from ....chat.choices.stream.types import AiChoicesDeltas # noqa
|
|
22
27
|
from ....chat.choices.types import AiChoice
|
|
23
28
|
from ....chat.choices.types import ChatChoicesOutputs
|
|
24
29
|
from ....chat.messages import AiMessage
|
|
@@ -27,12 +32,7 @@ from ....chat.messages import SystemMessage
|
|
|
27
32
|
from ....chat.messages import ToolUseMessage
|
|
28
33
|
from ....chat.messages import ToolUseResultMessage
|
|
29
34
|
from ....chat.messages import UserMessage
|
|
30
|
-
from ....chat.stream.
|
|
31
|
-
from ....chat.stream.services import ChatChoicesStreamResponse
|
|
32
|
-
from ....chat.stream.services import static_check_is_chat_choices_stream_service
|
|
33
|
-
from ....chat.stream.types import AiChoiceDeltas # noqa
|
|
34
|
-
from ....chat.stream.types import AiChoicesDeltas # noqa
|
|
35
|
-
from ....chat.stream.types import ContentAiChoiceDelta # noqa
|
|
35
|
+
from ....chat.stream.types import ContentAiDelta # noqa
|
|
36
36
|
from ....completion import CompletionRequest
|
|
37
37
|
from ....completion import CompletionResponse
|
|
38
38
|
from ....completion import static_check_is_completion_service
|
|
@@ -288,7 +288,7 @@ class TransformersChatChoicesStreamService(BaseTransformersChatChoicesService):
|
|
|
288
288
|
end = False
|
|
289
289
|
|
|
290
290
|
if out:
|
|
291
|
-
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([
|
|
291
|
+
await sink.emit(AiChoicesDeltas([AiChoiceDeltas([ContentAiDelta(out)])]))
|
|
292
292
|
|
|
293
293
|
if end:
|
|
294
294
|
break
|
|
File without changes
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
from omlish import dataclasses as dc
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
from ..
|
|
5
|
-
from ..
|
|
6
|
-
from ..
|
|
7
|
-
from ..
|
|
8
|
-
from .joining import
|
|
3
|
+
from ....services import Response
|
|
4
|
+
from ..services import ChatChoicesRequest
|
|
5
|
+
from ..services import static_check_is_chat_choices_service
|
|
6
|
+
from ..types import AiChoice
|
|
7
|
+
from ..types import AiChoices
|
|
8
|
+
from .joining import AiChoicesDeltaJoiner
|
|
9
9
|
from .services import ChatChoicesOutputs
|
|
10
10
|
from .services import ChatChoicesStreamOutputs
|
|
11
11
|
from .services import ChatChoicesStreamService
|
|
@@ -23,7 +23,7 @@ class ChatChoicesStreamServiceChatChoicesService:
|
|
|
23
23
|
AiChoices,
|
|
24
24
|
ChatChoicesOutputs | ChatChoicesStreamOutputs,
|
|
25
25
|
]:
|
|
26
|
-
joiner =
|
|
26
|
+
joiner = AiChoicesDeltaJoiner()
|
|
27
27
|
|
|
28
28
|
async with (resp := await self.service.invoke(request)).v as it: # noqa
|
|
29
29
|
async for cs in it:
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import typing as ta
|
|
2
|
+
|
|
3
|
+
from omlish import check
|
|
4
|
+
|
|
5
|
+
from ...messages import AiChat
|
|
6
|
+
from ...stream.joining import AiDeltaJoiner
|
|
7
|
+
from .types import AiChoiceDeltas
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
##
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AiChoicesDeltaJoiner:
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
super().__init__()
|
|
16
|
+
|
|
17
|
+
self._seq = 0
|
|
18
|
+
self._channels: list[AiDeltaJoiner] = []
|
|
19
|
+
|
|
20
|
+
def add(self, choices: ta.Sequence[AiChoiceDeltas]) -> None:
|
|
21
|
+
if not self._seq:
|
|
22
|
+
check.empty(self._channels)
|
|
23
|
+
self._channels.extend(AiDeltaJoiner() for _ in range(len(choices)))
|
|
24
|
+
|
|
25
|
+
for chan, c in zip(self._channels, choices, strict=True):
|
|
26
|
+
chan.add(c.deltas)
|
|
27
|
+
|
|
28
|
+
self._seq += 1
|
|
29
|
+
|
|
30
|
+
def build(self) -> list[AiChat]:
|
|
31
|
+
return [list(chan.build()) for chan in self._channels]
|