Skip to content

Commit c5d6b7b

Browse files
add python-langchain-tools generator to generate langchain tools from spec
1 parent 6f3daca commit c5d6b7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+11637
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
generatorName: python-langchain-tools
2+
outputDir: samples/client/petstore/python/langchain/tools
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/python-langchain-tools
5+
additionalProperties:
6+
hideGenerationTimestamp: "true"
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
---
2+
title: Documentation for the python-langchain-tools Generator
3+
---
4+
5+
## METADATA
6+
7+
| Property | Value | Notes |
8+
| -------- | ----- | ----- |
9+
| generator name | python-langchain-tools | pass this to the generate command after -g |
10+
| generator stability | STABLE | |
11+
| generator type | CLIENT | |
12+
| generator language | Python | |
13+
| generator language version | 3.9+ | |
14+
| generator default templating engine | mustache | |
15+
| helpTxt | Generates a Python client for LangChain agent tools, grouped by tags. | |
16+
17+
## CONFIG OPTIONS
18+
These options may be applied as additional-properties (cli) or configOptions (plugins). Refer to [configuration docs](https://openapi-generator.tech/docs/configuration) for more details.
19+
20+
| Option | Description | Values | Default |
21+
| ------ | ----------- | ------ | ------- |
22+
|dateFormat|date format for query parameters| |%Y-%m-%d|
23+
|datetimeFormat|datetime format for query parameters| |%Y-%m-%dT%H:%M:%S%z|
24+
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
25+
|generateSourceCodeOnly|Specifies that only a library source code is to be generated.| |false|
26+
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
27+
|lazyImports|Enable lazy imports.| |false|
28+
|library|library template (sub-template) to use: asyncio, tornado (deprecated), urllib3, httpx| |urllib3|
29+
|mapNumberTo|Map number to Union[StrictFloat, StrictInt], StrictStr or float.| |Union[StrictFloat, StrictInt]|
30+
|packageName|python package name (convention: snake_case).| |openapi_client|
31+
|packageUrl|python package URL.| |null|
32+
|packageVersion|python package version.| |1.0.0|
33+
|poetry1|Fallback to formatting pyproject.toml to Poetry 1.x format.| |null|
34+
|projectName|python project name in setup.py (e.g. petstore-api).| |null|
35+
|recursionLimit|Set the recursion limit. If not set, use the system default value.| |null|
36+
|setEnsureAsciiToFalse|When set to true, add `ensure_ascii=False` in json.dumps when creating the HTTP request body.| |false|
37+
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
38+
39+
## IMPORT MAPPING
40+
41+
| Type/Alias | Imports |
42+
| ---------- | ------- |
43+
44+
45+
## INSTANTIATION TYPES
46+
47+
| Type/Alias | Instantiated By |
48+
| ---------- | --------------- |
49+
50+
51+
## LANGUAGE PRIMITIVES
52+
53+
<ul class="column-ul">
54+
<li>Dict</li>
55+
<li>List</li>
56+
<li>bool</li>
57+
<li>bytearray</li>
58+
<li>bytes</li>
59+
<li>date</li>
60+
<li>datetime</li>
61+
<li>decimal.Decimal</li>
62+
<li>dict</li>
63+
<li>float</li>
64+
<li>int</li>
65+
<li>list</li>
66+
<li>none_type</li>
67+
<li>object</li>
68+
<li>str</li>
69+
</ul>
70+
71+
## RESERVED WORDS
72+
73+
<ul class="column-ul">
74+
<li>all_params</li>
75+
<li>and</li>
76+
<li>as</li>
77+
<li>assert</li>
78+
<li>async</li>
79+
<li>auth_settings</li>
80+
<li>await</li>
81+
<li>base64</li>
82+
<li>body_params</li>
83+
<li>break</li>
84+
<li>class</li>
85+
<li>continue</li>
86+
<li>date</li>
87+
<li>def</li>
88+
<li>del</li>
89+
<li>elif</li>
90+
<li>else</li>
91+
<li>except</li>
92+
<li>exec</li>
93+
<li>false</li>
94+
<li>field</li>
95+
<li>finally</li>
96+
<li>float</li>
97+
<li>for</li>
98+
<li>form_params</li>
99+
<li>from</li>
100+
<li>global</li>
101+
<li>header_params</li>
102+
<li>if</li>
103+
<li>import</li>
104+
<li>in</li>
105+
<li>is</li>
106+
<li>json</li>
107+
<li>lambda</li>
108+
<li>local_var_files</li>
109+
<li>none</li>
110+
<li>nonlocal</li>
111+
<li>not</li>
112+
<li>or</li>
113+
<li>pass</li>
114+
<li>path_params</li>
115+
<li>print</li>
116+
<li>property</li>
117+
<li>query_params</li>
118+
<li>raise</li>
119+
<li>resource_path</li>
120+
<li>return</li>
121+
<li>schema</li>
122+
<li>self</li>
123+
<li>true</li>
124+
<li>try</li>
125+
<li>while</li>
126+
<li>with</li>
127+
<li>yield</li>
128+
</ul>
129+
130+
## FEATURE SET
131+
132+
133+
### Client Modification Feature
134+
| Name | Supported | Defined By |
135+
| ---- | --------- | ---------- |
136+
|BasePath|✗|ToolingExtension
137+
|Authorizations|✗|ToolingExtension
138+
|UserAgent|✗|ToolingExtension
139+
|MockServer|✗|ToolingExtension
140+
141+
### Data Type Feature
142+
| Name | Supported | Defined By |
143+
| ---- | --------- | ---------- |
144+
|Custom|✗|OAS2,OAS3
145+
|Int32|✓|OAS2,OAS3
146+
|Int64|✓|OAS2,OAS3
147+
|Float|✓|OAS2,OAS3
148+
|Double|✓|OAS2,OAS3
149+
|Decimal|✓|ToolingExtension
150+
|String|✓|OAS2,OAS3
151+
|Byte|✓|OAS2,OAS3
152+
|Binary|✓|OAS2,OAS3
153+
|Boolean|✓|OAS2,OAS3
154+
|Date|✓|OAS2,OAS3
155+
|DateTime|✓|OAS2,OAS3
156+
|Password|✓|OAS2,OAS3
157+
|File|✓|OAS2
158+
|Uuid||
159+
|Array|✓|OAS2,OAS3
160+
|Null|✗|OAS3
161+
|AnyType|✗|OAS2,OAS3
162+
|Object|✓|OAS2,OAS3
163+
|Maps|✓|ToolingExtension
164+
|CollectionFormat|✓|OAS2
165+
|CollectionFormatMulti|✓|OAS2
166+
|Enum|✓|OAS2,OAS3
167+
|ArrayOfEnum|✓|ToolingExtension
168+
|ArrayOfModel|✓|ToolingExtension
169+
|ArrayOfCollectionOfPrimitives|✓|ToolingExtension
170+
|ArrayOfCollectionOfModel|✓|ToolingExtension
171+
|ArrayOfCollectionOfEnum|✓|ToolingExtension
172+
|MapOfEnum|✓|ToolingExtension
173+
|MapOfModel|✓|ToolingExtension
174+
|MapOfCollectionOfPrimitives|✓|ToolingExtension
175+
|MapOfCollectionOfModel|✓|ToolingExtension
176+
|MapOfCollectionOfEnum|✓|ToolingExtension
177+
178+
### Documentation Feature
179+
| Name | Supported | Defined By |
180+
| ---- | --------- | ---------- |
181+
|Readme|✓|ToolingExtension
182+
|Model|✓|ToolingExtension
183+
|Api|✓|ToolingExtension
184+
185+
### Global Feature
186+
| Name | Supported | Defined By |
187+
| ---- | --------- | ---------- |
188+
|Host|✓|OAS2,OAS3
189+
|BasePath|✓|OAS2,OAS3
190+
|Info|✓|OAS2,OAS3
191+
|Schemes|✗|OAS2,OAS3
192+
|PartialSchemes|✓|OAS2,OAS3
193+
|Consumes|✓|OAS2
194+
|Produces|✓|OAS2
195+
|ExternalDocumentation|✓|OAS2,OAS3
196+
|Examples|✓|OAS2,OAS3
197+
|XMLStructureDefinitions|✗|OAS2,OAS3
198+
|MultiServer|✗|OAS3
199+
|ParameterizedServer|✗|OAS3
200+
|ParameterStyling|✗|OAS3
201+
|Callbacks|✗|OAS3
202+
|LinkObjects|✗|OAS3
203+
204+
### Parameter Feature
205+
| Name | Supported | Defined By |
206+
| ---- | --------- | ---------- |
207+
|Path|✓|OAS2,OAS3
208+
|Query|✓|OAS2,OAS3
209+
|Header|✓|OAS2,OAS3
210+
|Body|✓|OAS2
211+
|FormUnencoded|✓|OAS2
212+
|FormMultipart|✓|OAS2
213+
|Cookie|✗|OAS3
214+
215+
### Schema Support Feature
216+
| Name | Supported | Defined By |
217+
| ---- | --------- | ---------- |
218+
|Simple|✓|OAS2,OAS3
219+
|Composite|✓|OAS2,OAS3
220+
|Polymorphism|✓|OAS2,OAS3
221+
|Union|✗|OAS3
222+
|allOf|✓|OAS2,OAS3
223+
|anyOf|✓|OAS3
224+
|oneOf|✓|OAS3
225+
|not|✗|OAS3
226+
227+
### Security Feature
228+
| Name | Supported | Defined By |
229+
| ---- | --------- | ---------- |
230+
|BasicAuth|✓|OAS2,OAS3
231+
|ApiKey|✓|OAS2,OAS3
232+
|OpenIDConnect|✗|OAS3
233+
|BearerToken|✓|OAS3
234+
|OAuth2_Implicit|✓|OAS2,OAS3
235+
|OAuth2_Password|✗|OAS2,OAS3
236+
|OAuth2_ClientCredentials|✗|OAS2,OAS3
237+
|OAuth2_AuthorizationCode|✗|OAS2,OAS3
238+
|SignatureAuth|✓|OAS3
239+
|AWSV4Signature|✗|ToolingExtension
240+
241+
### Wire Format Feature
242+
| Name | Supported | Defined By |
243+
| ---- | --------- | ---------- |
244+
|JSON|✓|OAS2,OAS3
245+
|XML|✓|OAS2,OAS3
246+
|PROTOBUF|✗|ToolingExtension
247+
|Custom|✓|OAS2,OAS3
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.openapitools.codegen.languages;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
6+
import java.nio.file.Files;
7+
import java.nio.file.StandardOpenOption;
8+
9+
import org.openapitools.codegen.SupportingFile;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class PythonLangchainToolsClientCodegen extends PythonClientCodegen {
14+
public static final String PROJECT_NAME = "projectName";
15+
16+
private final Logger LOGGER = LoggerFactory.getLogger(PythonLangchainToolsClientCodegen.class);
17+
18+
public PythonLangchainToolsClientCodegen() {
19+
super();
20+
apiTemplateFiles.put("api_tools.mustache", "_tools.py");
21+
supportingFiles.add(new SupportingFile("all_tools.mustache", "", "all_tools.py"));
22+
}
23+
24+
@Override
25+
public String getName() {
26+
return "python-langchain-tools";
27+
}
28+
29+
@Override
30+
public String getHelp() {
31+
return "Generates a Python client for LangChain agent tools, grouped by tags.";
32+
}
33+
34+
@Override
35+
public String apiFileFolder() {
36+
return outputFolder + File.separator + apiPackage().replace('.', File.separatorChar);
37+
}
38+
39+
@Override
40+
public String modelFileFolder() {
41+
return outputFolder + File.separator + modelPackage().replace('.', File.separatorChar);
42+
}
43+
44+
@Override
45+
public void processOpts() {
46+
super.processOpts();
47+
48+
// Now that packageName is processed and available, we can add the supporting file
49+
// for the tools package __init__.py in the correct directory.
50+
final String toolsPackagePath = packageName.replace(".", File.separator) + File.separator + "tools";
51+
supportingFiles.add(new SupportingFile("init.mustache", toolsPackagePath, "__init__.py"));
52+
}
53+
54+
@Override
55+
public boolean isEnablePostProcessFile() {
56+
return true;
57+
}
58+
59+
/**
60+
* This is a way to add a dependency without duplicating the parent template.
61+
*/
62+
@Override
63+
public void postProcessFile(File file, String fileType) {
64+
super.postProcessFile(file, fileType);
65+
if (file == null) {
66+
return;
67+
}
68+
final String filename = file.getName();
69+
if ("requirements.txt".equals(filename)) {
70+
try {
71+
String langchainDep = "\nlangchain >=0.3, <0.4\n";
72+
Files.write(file.toPath(), langchainDep.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
73+
} catch (IOException e) {
74+
throw new RuntimeException("Unable to write to requirements.txt", e);
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Overriding this method to change the output location of our tool files.
81+
*/
82+
@Override
83+
public String apiFilename(String templateName, String tag) {
84+
final String originalFilename = super.apiFilename(templateName, tag);
85+
86+
if ("api_tools.mustache".equals(templateName)) {
87+
return originalFilename.replace(apiPackage().replace(".", File.separator),
88+
packageName.replace(".", File.separator) + File.separator + "tools");
89+
}
90+
91+
return originalFilename;
92+
}
93+
94+
}

modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ org.openapitools.codegen.languages.PythonFastAPIServerCodegen
118118
org.openapitools.codegen.languages.PythonFlaskConnexionServerCodegen
119119
org.openapitools.codegen.languages.PythonAiohttpConnexionServerCodegen
120120
org.openapitools.codegen.languages.PythonBluePlanetServerCodegen
121+
org.openapitools.codegen.languages.PythonLangchainToolsClientCodegen
121122
org.openapitools.codegen.languages.RClientCodegen
122123
org.openapitools.codegen.languages.RubyClientCodegen
123124
org.openapitools.codegen.languages.RubyOnRailsServerCodegen

modules/openapi-generator/src/main/resources/python-langchain-tools/README.mustache

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
An aggregator for all generated LangChain tools.
4+
5+
This file provides a single list, `all_tools`, that you can import and
6+
provide to your LangChain agent.
7+
"""
8+
from typing import List
9+
from langchain.tools import StructuredTool
10+
11+
from {{packageName}}.api_client import ApiClient
12+
from {{packageName}}.configuration import Configuration
13+
14+
# Import the factory functions from each tool module
15+
{{#apiInfo}}
16+
{{#apis}}
17+
from {{packageName}}.tools.{{classFilename}}_tools import get_{{classVarName}}_tools
18+
{{/apis}}
19+
{{/apiInfo}}
20+
21+
def get_all_tools() -> List[StructuredTool]:
22+
"""
23+
Initializes the API client and aggregates tools from all API modules.
24+
"""
25+
# TODO: You may need to customize this based on your API's authentication needs.
26+
configuration = Configuration(host="{{{serverUrl}}}")
27+
28+
all_tools_list = []
29+
30+
with ApiClient(configuration) as api_client:
31+
{{#apiInfo}}
32+
{{#apis}}
33+
all_tools_list.extend(get_{{classVarName}}_tools(api_client))
34+
{{/apis}}
35+
{{/apiInfo}}
36+
37+
return all_tools_list
38+
39+
# A pre-initialized list of all tools for convenience.
40+
all_tools = get_all_tools()

0 commit comments

Comments
 (0)