llm-codegen-research 2.12__tar.gz → 2.13__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.
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/PKG-INFO +1 -1
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/__init__.py +4 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/__init__.py +4 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/__init__.py +22 -0
- llm_codegen_research-2.13/src/llm_cgr/llm/clients/openai_tool.py +258 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/PKG-INFO +1 -1
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/SOURCES.txt +2 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/tests/test_llm_local.py +26 -0
- llm_codegen_research-2.13/tests/test_llm_tool.py +96 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/LICENSE +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/README.md +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/pyproject.toml +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/setup.cfg +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/__init__.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/classes.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/__init__.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/code_data.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/javascript.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/python.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/rust.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/regexes.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/decorators.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/defaults.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/enums.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/json_utils.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/anthropic.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/base.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/deepseek.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/mistral.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/nscale.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/openai.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/protocol.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/together.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/generate.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/prompts.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/py.typed +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/scripts/test_cuda.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/timeout.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/dependency_links.txt +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/entry_points.txt +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/requires.txt +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/top_level.txt +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/tests/test_enums.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/tests/test_json_utils.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/tests/test_llm_api.py +0 -0
- {llm_codegen_research-2.12 → llm_codegen_research-2.13}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: llm-codegen-research
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.13
|
|
4
4
|
Summary: Useful classes and methods for researching code-generation by LLMs.
|
|
5
5
|
Author-email: Lukas Twist <itsluketwist@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/itsluketwist/llm-codegen-research
|
|
@@ -45,7 +45,9 @@ try:
|
|
|
45
45
|
GenerationProtocol,
|
|
46
46
|
Mistral_LLM,
|
|
47
47
|
OpenAI_LLM,
|
|
48
|
+
OpenAI_Tool_LLM,
|
|
48
49
|
TogetherAI_LLM,
|
|
50
|
+
Tool,
|
|
49
51
|
generate,
|
|
50
52
|
generate_bool,
|
|
51
53
|
generate_list,
|
|
@@ -64,6 +66,8 @@ try:
|
|
|
64
66
|
"GenerationProtocol",
|
|
65
67
|
"Mistral_LLM",
|
|
66
68
|
"OpenAI_LLM",
|
|
69
|
+
"OpenAI_Tool_LLM",
|
|
70
|
+
"Tool",
|
|
67
71
|
"TogetherAI_LLM",
|
|
68
72
|
"generate",
|
|
69
73
|
"generate_bool",
|
|
@@ -5,7 +5,9 @@ from llm_cgr.llm.clients import (
|
|
|
5
5
|
GenerationProtocol,
|
|
6
6
|
Mistral_LLM,
|
|
7
7
|
OpenAI_LLM,
|
|
8
|
+
OpenAI_Tool_LLM,
|
|
8
9
|
TogetherAI_LLM,
|
|
10
|
+
Tool,
|
|
9
11
|
get_llm,
|
|
10
12
|
)
|
|
11
13
|
from llm_cgr.llm.generate import generate, generate_bool, generate_list
|
|
@@ -24,6 +26,8 @@ __all__ = [
|
|
|
24
26
|
"GenerationProtocol",
|
|
25
27
|
"Mistral_LLM",
|
|
26
28
|
"OpenAI_LLM",
|
|
29
|
+
"OpenAI_Tool_LLM",
|
|
30
|
+
"Tool",
|
|
27
31
|
"TogetherAI_LLM",
|
|
28
32
|
"get_llm",
|
|
29
33
|
"generate",
|
|
@@ -6,6 +6,7 @@ from llm_cgr.llm.clients.deepseek import DeepSeek_LLM
|
|
|
6
6
|
from llm_cgr.llm.clients.mistral import Mistral_LLM
|
|
7
7
|
from llm_cgr.llm.clients.nscale import Nscale_LLM
|
|
8
8
|
from llm_cgr.llm.clients.openai import OpenAI_LLM
|
|
9
|
+
from llm_cgr.llm.clients.openai_tool import OpenAI_Tool_LLM, Tool
|
|
9
10
|
from llm_cgr.llm.clients.protocol import GenerationProtocol
|
|
10
11
|
from llm_cgr.llm.clients.together import TogetherAI_LLM
|
|
11
12
|
|
|
@@ -27,9 +28,13 @@ def get_llm(
|
|
|
27
28
|
top_p: float | None = None,
|
|
28
29
|
max_tokens: int | None = None,
|
|
29
30
|
provider: str | None = None,
|
|
31
|
+
tools: list[Tool] | None = None,
|
|
30
32
|
) -> GenerationProtocol:
|
|
31
33
|
"""
|
|
32
34
|
Initialise the correct LLM client for the given model.
|
|
35
|
+
|
|
36
|
+
If tools are provided, returns an OpenAI_Tool_LLM instance. Tool calls
|
|
37
|
+
are currently only supported for OpenAI models.
|
|
33
38
|
"""
|
|
34
39
|
llm_class: type[Base_LLM]
|
|
35
40
|
if provider is not None:
|
|
@@ -45,6 +50,21 @@ def get_llm(
|
|
|
45
50
|
else:
|
|
46
51
|
llm_class = TogetherAI_LLM
|
|
47
52
|
|
|
53
|
+
# if tools are requested, use the tool-enabled subclass (openai only for now)
|
|
54
|
+
if tools is not None:
|
|
55
|
+
if llm_class is not OpenAI_LLM:
|
|
56
|
+
raise NotImplementedError(
|
|
57
|
+
"Tool calls are only supported for OpenAI models."
|
|
58
|
+
)
|
|
59
|
+
return OpenAI_Tool_LLM(
|
|
60
|
+
tools=tools,
|
|
61
|
+
model=model,
|
|
62
|
+
system=system,
|
|
63
|
+
temperature=temperature,
|
|
64
|
+
top_p=top_p,
|
|
65
|
+
max_tokens=max_tokens,
|
|
66
|
+
)
|
|
67
|
+
|
|
48
68
|
return llm_class(
|
|
49
69
|
model=model,
|
|
50
70
|
system=system,
|
|
@@ -60,7 +80,9 @@ __all__ = [
|
|
|
60
80
|
"DeepSeek_LLM",
|
|
61
81
|
"GenerationProtocol",
|
|
62
82
|
"OpenAI_LLM",
|
|
83
|
+
"OpenAI_Tool_LLM",
|
|
63
84
|
"TogetherAI_LLM",
|
|
64
85
|
"Mistral_LLM",
|
|
86
|
+
"Tool",
|
|
65
87
|
"get_llm",
|
|
66
88
|
]
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"""OpenAI client subclass with an agentic tool-call loop."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Any, Callable, cast
|
|
6
|
+
|
|
7
|
+
import openai
|
|
8
|
+
from openai.types.responses import ResponseFunctionToolCall, ResponseInputItemParam
|
|
9
|
+
|
|
10
|
+
from llm_cgr.llm.clients.openai import OpenAI_LLM
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# maximum number of tool-call iterations per request, to prevent runaway loops
|
|
14
|
+
MAX_TOOL_ITERATIONS: int = 10
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class Tool:
|
|
19
|
+
"""
|
|
20
|
+
A tool (function) that the model can call during generation.
|
|
21
|
+
|
|
22
|
+
Attributes:
|
|
23
|
+
name: The function name the model uses to call this tool.
|
|
24
|
+
description: Describes what the tool does; the model uses this
|
|
25
|
+
to decide when to call it.
|
|
26
|
+
parameters: A JSON schema dict describing the function's parameters.
|
|
27
|
+
fn: The Python callable to invoke; must accept kwargs matching the
|
|
28
|
+
schema and return a str result.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
name: str
|
|
32
|
+
description: str
|
|
33
|
+
parameters: dict[str, Any]
|
|
34
|
+
fn: Callable[..., str]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class OpenAI_Tool_LLM(OpenAI_LLM):
|
|
38
|
+
"""OpenAI client with an agentic tool-call loop.
|
|
39
|
+
|
|
40
|
+
Tools are supplied at construction time and used for all subsequent
|
|
41
|
+
generate() and chat() calls. The client handles the full loop internally:
|
|
42
|
+
call the API, execute any tool calls, feed results back, repeat until the
|
|
43
|
+
model produces a final text response.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
tools: list[Tool],
|
|
49
|
+
model: str | None = None,
|
|
50
|
+
system: str | None = None,
|
|
51
|
+
temperature: float | None = None,
|
|
52
|
+
top_p: float | None = None,
|
|
53
|
+
max_tokens: int | None = None,
|
|
54
|
+
) -> None:
|
|
55
|
+
"""
|
|
56
|
+
Initialise the OpenAI tool client.
|
|
57
|
+
|
|
58
|
+
Requires the OPENAI_API_KEY environment variable to be set.
|
|
59
|
+
"""
|
|
60
|
+
super().__init__(
|
|
61
|
+
model=model,
|
|
62
|
+
system=system,
|
|
63
|
+
temperature=temperature,
|
|
64
|
+
top_p=top_p,
|
|
65
|
+
max_tokens=max_tokens,
|
|
66
|
+
)
|
|
67
|
+
self._tools = tools
|
|
68
|
+
# cumulative count of individual tool calls made by this instance
|
|
69
|
+
self._tool_calls: int = 0
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def tool_calls(self) -> int:
|
|
73
|
+
"""Total number of tool calls made by this client since instantiation.
|
|
74
|
+
|
|
75
|
+
Returns the cumulative count across all generate() and chat() calls.
|
|
76
|
+
Tip: record the value before a call and subtract to get the count for
|
|
77
|
+
that specific call.
|
|
78
|
+
"""
|
|
79
|
+
return self._tool_calls
|
|
80
|
+
|
|
81
|
+
def _build_tool_param(
|
|
82
|
+
self,
|
|
83
|
+
tool: Tool,
|
|
84
|
+
) -> dict[str, Any]:
|
|
85
|
+
"""Convert a Tool dataclass to the dict format the OpenAI Responses API expects."""
|
|
86
|
+
return {
|
|
87
|
+
"type": "function",
|
|
88
|
+
"name": tool.name,
|
|
89
|
+
"description": tool.description,
|
|
90
|
+
"parameters": tool.parameters,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
def _run_tool_loop(
|
|
94
|
+
self,
|
|
95
|
+
messages: list[dict[str, Any]],
|
|
96
|
+
model: str,
|
|
97
|
+
temperature: float | None,
|
|
98
|
+
top_p: float | None,
|
|
99
|
+
max_tokens: int | None,
|
|
100
|
+
) -> str:
|
|
101
|
+
"""Run the agentic tool-call loop for a single turn.
|
|
102
|
+
|
|
103
|
+
Calls the OpenAI API in a loop, executing any tool calls the model
|
|
104
|
+
requests, until the model produces a final text response or the
|
|
105
|
+
MAX_TOOL_ITERATIONS safety limit is reached.
|
|
106
|
+
|
|
107
|
+
Returns the final text response.
|
|
108
|
+
"""
|
|
109
|
+
# convert Tool dataclasses to the API's function-tool format
|
|
110
|
+
api_tools = [self._build_tool_param(t) for t in self._tools]
|
|
111
|
+
|
|
112
|
+
# build a name -> Tool lookup map for fast dispatch during the loop
|
|
113
|
+
tool_map = {t.name: t for t in self._tools}
|
|
114
|
+
|
|
115
|
+
# shallow copy so intermediate tool-call scaffolding never mutates the
|
|
116
|
+
# caller's message list (prevents corruption of the chat history).
|
|
117
|
+
# typed as list[Any] so we can freely append both plain message dicts
|
|
118
|
+
# and the richer tool-call dicts without fighting the type checker.
|
|
119
|
+
current_input: list[Any] = list(messages)
|
|
120
|
+
|
|
121
|
+
for _ in range(MAX_TOOL_ITERATIONS):
|
|
122
|
+
response = self._client.responses.create(
|
|
123
|
+
input=cast(list[ResponseInputItemParam], current_input),
|
|
124
|
+
model=model,
|
|
125
|
+
temperature=temperature if temperature is not None else openai.omit,
|
|
126
|
+
top_p=top_p if top_p is not None else openai.omit,
|
|
127
|
+
max_output_tokens=max_tokens if max_tokens is not None else openai.omit,
|
|
128
|
+
tools=cast(Any, api_tools),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
# collect any function calls the model requested in this response
|
|
132
|
+
function_calls = [
|
|
133
|
+
item for item in response.output if item.type == "function_call"
|
|
134
|
+
]
|
|
135
|
+
|
|
136
|
+
# no tool calls means the model has produced its final text answer
|
|
137
|
+
if not function_calls:
|
|
138
|
+
return response.output_text
|
|
139
|
+
|
|
140
|
+
# increment the cumulative counter; parallel calls count individually
|
|
141
|
+
self._tool_calls += len(function_calls)
|
|
142
|
+
|
|
143
|
+
# process each tool call: the OpenAI Responses API requires that the
|
|
144
|
+
# function_call item appears in the next input before its matching
|
|
145
|
+
# function_call_output item
|
|
146
|
+
for _call in function_calls:
|
|
147
|
+
# cast to the concrete type so we can access .call_id/.name/.arguments
|
|
148
|
+
call = cast(ResponseFunctionToolCall, _call)
|
|
149
|
+
|
|
150
|
+
# append the function_call itself so the model sees what it called
|
|
151
|
+
current_input.append(
|
|
152
|
+
{
|
|
153
|
+
"type": "function_call",
|
|
154
|
+
"call_id": call.call_id,
|
|
155
|
+
"name": call.name,
|
|
156
|
+
"arguments": call.arguments,
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
# deserialise the model's json argument string and call the local fn
|
|
161
|
+
kwargs = json.loads(call.arguments)
|
|
162
|
+
result = tool_map[call.name].fn(**kwargs)
|
|
163
|
+
|
|
164
|
+
# append the result so the model can read it on the next turn
|
|
165
|
+
current_input.append(
|
|
166
|
+
{
|
|
167
|
+
"type": "function_call_output",
|
|
168
|
+
"call_id": call.call_id,
|
|
169
|
+
"output": result,
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# loop continues: enriched input is sent back to the model
|
|
174
|
+
|
|
175
|
+
# safety fallback: return whatever text the model produced on the last turn
|
|
176
|
+
return response.output_text
|
|
177
|
+
|
|
178
|
+
def generate(
|
|
179
|
+
self,
|
|
180
|
+
user: str,
|
|
181
|
+
system: str | None = None,
|
|
182
|
+
model: str | None = None,
|
|
183
|
+
samples: int = 1,
|
|
184
|
+
temperature: float | None = None,
|
|
185
|
+
top_p: float | None = None,
|
|
186
|
+
max_tokens: int | None = None,
|
|
187
|
+
) -> list[str]:
|
|
188
|
+
"""Generate model responses via the agentic tool-call loop."""
|
|
189
|
+
_model = model or self._model
|
|
190
|
+
if _model is None:
|
|
191
|
+
raise ValueError("Model must be specified for LLM APIs.")
|
|
192
|
+
|
|
193
|
+
messages = self._build_input(
|
|
194
|
+
user=user,
|
|
195
|
+
system=system or self._system,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
_generations = []
|
|
199
|
+
for _ in range(samples):
|
|
200
|
+
result = self._run_tool_loop(
|
|
201
|
+
messages=messages,
|
|
202
|
+
model=_model,
|
|
203
|
+
temperature=temperature or self._temperature,
|
|
204
|
+
top_p=top_p or self._top_p,
|
|
205
|
+
max_tokens=max_tokens or self._max_tokens,
|
|
206
|
+
)
|
|
207
|
+
_generations.append(result)
|
|
208
|
+
|
|
209
|
+
return _generations
|
|
210
|
+
|
|
211
|
+
def chat(
|
|
212
|
+
self,
|
|
213
|
+
user: str,
|
|
214
|
+
system: str | None = None,
|
|
215
|
+
model: str | None = None,
|
|
216
|
+
temperature: float | None = None,
|
|
217
|
+
top_p: float | None = None,
|
|
218
|
+
max_tokens: int | None = None,
|
|
219
|
+
) -> str:
|
|
220
|
+
"""Run a chat turn via the agentic tool-call loop.
|
|
221
|
+
|
|
222
|
+
Manages self._history identically to the base class — only the final
|
|
223
|
+
text response is appended, not intermediate tool-call scaffolding.
|
|
224
|
+
"""
|
|
225
|
+
_model = model or self._model
|
|
226
|
+
if _model is None:
|
|
227
|
+
raise ValueError("Model must be specified for LLM APIs.")
|
|
228
|
+
|
|
229
|
+
if self._history is None:
|
|
230
|
+
self._history = self._build_input(
|
|
231
|
+
user=user,
|
|
232
|
+
system=system or self._system,
|
|
233
|
+
)
|
|
234
|
+
else:
|
|
235
|
+
self._history.append(
|
|
236
|
+
self._build_message(
|
|
237
|
+
role="user",
|
|
238
|
+
content=user,
|
|
239
|
+
)
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# _run_tool_loop operates on a shallow copy of self._history, so
|
|
243
|
+
# intermediate tool-call items never appear in the chat history
|
|
244
|
+
response = self._run_tool_loop(
|
|
245
|
+
messages=self._history,
|
|
246
|
+
model=_model,
|
|
247
|
+
temperature=temperature or self._temperature,
|
|
248
|
+
top_p=top_p or self._top_p,
|
|
249
|
+
max_tokens=max_tokens or self._max_tokens,
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
self._history.append(
|
|
253
|
+
self._build_message(
|
|
254
|
+
role="assistant",
|
|
255
|
+
content=response,
|
|
256
|
+
)
|
|
257
|
+
)
|
|
258
|
+
return response
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_codegen_research.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: llm-codegen-research
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.13
|
|
4
4
|
Summary: Useful classes and methods for researching code-generation by LLMs.
|
|
5
5
|
Author-email: Lukas Twist <itsluketwist@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/itsluketwist/llm-codegen-research
|
|
@@ -26,6 +26,7 @@ src/llm_cgr/llm/clients/deepseek.py
|
|
|
26
26
|
src/llm_cgr/llm/clients/mistral.py
|
|
27
27
|
src/llm_cgr/llm/clients/nscale.py
|
|
28
28
|
src/llm_cgr/llm/clients/openai.py
|
|
29
|
+
src/llm_cgr/llm/clients/openai_tool.py
|
|
29
30
|
src/llm_cgr/llm/clients/protocol.py
|
|
30
31
|
src/llm_cgr/llm/clients/together.py
|
|
31
32
|
src/llm_cgr/scripts/test_cuda.py
|
|
@@ -39,4 +40,5 @@ tests/test_enums.py
|
|
|
39
40
|
tests/test_json_utils.py
|
|
40
41
|
tests/test_llm_api.py
|
|
41
42
|
tests/test_llm_local.py
|
|
43
|
+
tests/test_llm_tool.py
|
|
42
44
|
tests/test_utils.py
|
|
@@ -10,8 +10,10 @@ from llm_cgr import (
|
|
|
10
10
|
Mistral_LLM,
|
|
11
11
|
OpenAI_LLM,
|
|
12
12
|
TogetherAI_LLM,
|
|
13
|
+
Tool,
|
|
13
14
|
generate_bool,
|
|
14
15
|
generate_list,
|
|
16
|
+
get_llm,
|
|
15
17
|
)
|
|
16
18
|
|
|
17
19
|
|
|
@@ -129,6 +131,30 @@ def test_build_input():
|
|
|
129
131
|
]
|
|
130
132
|
|
|
131
133
|
|
|
134
|
+
def test_tools_unsupported_provider():
|
|
135
|
+
"""
|
|
136
|
+
Test that passing tools to a non-OpenAI provider raises NotImplementedError.
|
|
137
|
+
|
|
138
|
+
No API call is made because the error fires inside get_llm() before any
|
|
139
|
+
network request.
|
|
140
|
+
"""
|
|
141
|
+
dummy_tool = Tool(
|
|
142
|
+
name="dummy",
|
|
143
|
+
description="A dummy tool.",
|
|
144
|
+
parameters={"type": "object", "properties": {}},
|
|
145
|
+
fn=lambda: "result",
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# anthropic is not yet supported for tool calls
|
|
149
|
+
with pytest.raises(
|
|
150
|
+
NotImplementedError, match="Tool calls are only supported for OpenAI models."
|
|
151
|
+
):
|
|
152
|
+
get_llm(
|
|
153
|
+
model="claude-3-5-haiku-20241022",
|
|
154
|
+
tools=[dummy_tool],
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
|
|
132
158
|
@pytest.mark.parametrize(
|
|
133
159
|
"response,error",
|
|
134
160
|
[
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Test the OpenAI_Tool_LLM agentic tool-call loop."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from llm_cgr import OpenAI_Tool_LLM, Tool
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# mark all tests in this file as api tests, so they can be excluded in ci
|
|
9
|
+
pytestmark = pytest.mark.api
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_tool_call_generate(openai_model):
|
|
13
|
+
"""
|
|
14
|
+
Test that the OpenAI tool client runs the agentic loop and returns the
|
|
15
|
+
correct answer via the tool.
|
|
16
|
+
|
|
17
|
+
Uses an addition tool: the model must call it to get the answer, so we can
|
|
18
|
+
verify a real tool call happened (the model cannot guess what our local
|
|
19
|
+
function returns without calling it).
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def add(a: int, b: int) -> str:
|
|
23
|
+
"""Add two integers and return the result as a string."""
|
|
24
|
+
return str(a + b)
|
|
25
|
+
|
|
26
|
+
add_tool = Tool(
|
|
27
|
+
name="add",
|
|
28
|
+
description="Add two integers together and return the result.",
|
|
29
|
+
parameters={
|
|
30
|
+
"type": "object",
|
|
31
|
+
"properties": {
|
|
32
|
+
"a": {"type": "integer", "description": "The first integer."},
|
|
33
|
+
"b": {"type": "integer", "description": "The second integer."},
|
|
34
|
+
},
|
|
35
|
+
"required": ["a", "b"],
|
|
36
|
+
"additionalProperties": False,
|
|
37
|
+
},
|
|
38
|
+
fn=add,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
llm = OpenAI_Tool_LLM(tools=[add_tool], model=openai_model)
|
|
42
|
+
responses = llm.generate(
|
|
43
|
+
user="Use the add tool to compute 3 + 4. What is the result?"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
assert isinstance(responses, list)
|
|
47
|
+
assert len(responses) == 1
|
|
48
|
+
|
|
49
|
+
result = responses[0]
|
|
50
|
+
assert isinstance(result, str)
|
|
51
|
+
assert len(result) > 0
|
|
52
|
+
# the correct sum proves the tool was actually called
|
|
53
|
+
assert "7" in result
|
|
54
|
+
assert llm.tool_calls >= 1
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_tool_call_chat(openai_model):
|
|
58
|
+
"""
|
|
59
|
+
Test that tool calls work correctly in a chat session, and that
|
|
60
|
+
intermediate tool-call scaffolding does not corrupt the chat history.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def multiply(a: int, b: int) -> str:
|
|
64
|
+
"""Multiply two integers and return the result as a string."""
|
|
65
|
+
return str(a * b)
|
|
66
|
+
|
|
67
|
+
multiply_tool = Tool(
|
|
68
|
+
name="multiply",
|
|
69
|
+
description="Multiply two integers together and return the result.",
|
|
70
|
+
parameters={
|
|
71
|
+
"type": "object",
|
|
72
|
+
"properties": {
|
|
73
|
+
"a": {"type": "integer", "description": "The first integer."},
|
|
74
|
+
"b": {"type": "integer", "description": "The second integer."},
|
|
75
|
+
},
|
|
76
|
+
"required": ["a", "b"],
|
|
77
|
+
"additionalProperties": False,
|
|
78
|
+
},
|
|
79
|
+
fn=multiply,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
llm = OpenAI_Tool_LLM(tools=[multiply_tool], model=openai_model)
|
|
83
|
+
response = llm.chat(
|
|
84
|
+
user="Use the multiply tool to compute 6 * 7. What is the result?"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
assert isinstance(response, str)
|
|
88
|
+
assert "42" in response
|
|
89
|
+
|
|
90
|
+
# history should only contain the user turn and the final assistant response;
|
|
91
|
+
# no intermediate function_call or function_call_output items should have leaked in
|
|
92
|
+
history = llm.history
|
|
93
|
+
assert len(history) == 2
|
|
94
|
+
assert history[0]["role"] == "user"
|
|
95
|
+
assert history[1]["role"] == "assistant"
|
|
96
|
+
assert llm.tool_calls >= 1
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/__init__.py
RENAMED
|
File without changes
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/code_data.py
RENAMED
|
File without changes
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/javascript.py
RENAMED
|
File without changes
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/python.py
RENAMED
|
File without changes
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/analyse/languages/rust.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{llm_codegen_research-2.12 → llm_codegen_research-2.13}/src/llm_cgr/llm/clients/anthropic.py
RENAMED
|
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
|
|
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
|
|
File without changes
|