genaicode 0.0.31
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.
- package/LICENSE +202 -0
- package/README.md +130 -0
- package/bin/genaicode.cjs +8 -0
- package/bin/genaicode.js +5 -0
- package/bin/vertex-monkey-patch.cjs +33 -0
- package/media/demo-for-readme.gif +0 -0
- package/media/logo-dark.png +0 -0
- package/media/logo.png +0 -0
- package/package.json +58 -0
- package/src/ai-service/anthropic.js +91 -0
- package/src/ai-service/chat-gpt.js +96 -0
- package/src/ai-service/common.js +40 -0
- package/src/ai-service/dall-e.js +29 -0
- package/src/ai-service/function-calling.js +320 -0
- package/src/ai-service/vertex-ai-claude.js +95 -0
- package/src/ai-service/vertex-ai.js +155 -0
- package/src/cli/cli-options.js +106 -0
- package/src/cli/cli-options.test.js +25 -0
- package/src/cli/cli-params.js +84 -0
- package/src/cli/cli-params.test.js +43 -0
- package/src/cli/service-autodetect.js +11 -0
- package/src/cli/service-autodetect.test.js +43 -0
- package/src/cli/validate-cli-params.js +90 -0
- package/src/cli/validate-cli-params.test.js +103 -0
- package/src/files/find-files.js +148 -0
- package/src/files/read-files.js +41 -0
- package/src/files/update-files.js +118 -0
- package/src/main/codegen.js +113 -0
- package/src/main/codegen.test.js +186 -0
- package/src/prompt/limits.js +23 -0
- package/src/prompt/limits.test.js +40 -0
- package/src/prompt/prompt-codegen.js +106 -0
- package/src/prompt/prompt-codegen.test.js +116 -0
- package/src/prompt/prompt-consts.js +1 -0
- package/src/prompt/prompt-service.js +211 -0
- package/src/prompt/prompt-service.test.js +486 -0
- package/src/prompt/systemprompt.js +32 -0
- package/src/prompt/systemprompt.test.js +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="media/logo-dark.png">
|
|
4
|
+
<source media="(prefers-color-scheme: light)" srcset="media/logo.png">
|
|
5
|
+
<img alt="GenAIcode Logo." src="media/logo.png" width="100%" height="auto">
|
|
6
|
+
</picture>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
|
|
11
|
+
# Programming on steroids
|
|
12
|
+
|
|
13
|
+
<a href="https://www.npmjs.com/package/genaicode">
|
|
14
|
+
<img alt="npm version" src="https://img.shields.io/npm/v/genaicode.svg?style=flat-square"></a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/genaicode">
|
|
16
|
+
<img alt="weekly downloads from npm" src="https://img.shields.io/npm/dw/genaicode.svg?style=flat-square"></a>
|
|
17
|
+
<a href="https://github.com/gtanczyk/genaicode/actions?query=branch%3Amaster">
|
|
18
|
+
<img alt="Github Actions Build Status" src="https://img.shields.io/github/actions/workflow/status/gtanczyk/genaicode/lint.yaml?label=Tests&style=flat-square"></a>
|
|
19
|
+
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
### 🚧 GenAIcode is under development, use at own risk, feedback is welcomed! 🚧
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
The GenAIcode tool is designed to automate code generation tasks using various AI models. This tool enhances developer productivity by assisting with the generation of repetitive or complex code.
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
1. Create a `.genaicoderc` file in your project folder:
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
echo '{"rootDir": "."}' > .genaicoderc
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
2. Set up AI service credentials:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# For Vertex AI:
|
|
42
|
+
gcloud auth login
|
|
43
|
+
export GOOGLE_CLOUD_PROJECT="..."
|
|
44
|
+
|
|
45
|
+
# For Claude via Vertex AI:
|
|
46
|
+
gcloud auth login
|
|
47
|
+
export GOOGLE_CLOUD_PROJECT="..."
|
|
48
|
+
export GOOGLE_CLOUD_REGION="..."
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
3. Run GenAIcode:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npx genaicode --dry-run --explicit-prompt="Analyze my project sourcecode and write it to HELLO_GENAICODE.md" --consider-all-files
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## CLI Features
|
|
58
|
+
|
|
59
|
+
GenAIcode supports various command-line options to customize its behavior:
|
|
60
|
+
|
|
61
|
+
- `--dry-run`: Runs the tool without making any changes to the files.
|
|
62
|
+
- `--consider-all-files`: Considers all files for code generation, even if they don't contain `@CODEGEN` comments.
|
|
63
|
+
- `--allow-file-create`: Allows the tool to create new files.
|
|
64
|
+
- `--allow-file-delete`: Allows the tool to delete files.
|
|
65
|
+
- `--allow-directory-create`: Allows the tool to create directories.
|
|
66
|
+
- `--allow-file-move`: Allows the tool to move files within the project structure.
|
|
67
|
+
- `--vertex-ai`: Uses Vertex AI with Google's Gemini Pro model for code generation.
|
|
68
|
+
- `--vertex-ai-claude`: Uses Claude via Vertex AI for code generation.
|
|
69
|
+
- `--explicit-prompt=<prompt>`: Provides an explicit prompt for code generation.
|
|
70
|
+
- `--task-file=<file>`: Specifies a file with a task description for code generation.
|
|
71
|
+
- `--dependency-tree`: Limits the scope of code generation to files marked with `@CODEGEN` and their dependencies.
|
|
72
|
+
- `--verbose-prompt`: Prints the prompt used for code generation.
|
|
73
|
+
- `--require-explanations`: Requires explanations for all code generation operations.
|
|
74
|
+
- `--disable-context-optimization`: Disables the optimization that uses context paths for more efficient code generation.
|
|
75
|
+
- `--gemini-block-none`: Disables safety settings for Gemini Pro model (requires whitelisted Cloud project).
|
|
76
|
+
- `--disable-initial-lint`: Skips the initial lint check before running the code generation process.
|
|
77
|
+
- `--temperature=<value>`: Sets the temperature parameter for the AI model (default: 0.7).
|
|
78
|
+
- `--vision`: Enables vision capabilities for processing image inputs.
|
|
79
|
+
- `--imagen`: Enables image generation capabilities using AI models.
|
|
80
|
+
- `--help`: Displays the help message with all available options.
|
|
81
|
+
|
|
82
|
+
## Configuration (.genaicoderc)
|
|
83
|
+
|
|
84
|
+
The `.genaicoderc` file allows you to configure various aspects of GenAIcode's behavior. Here are the available options:
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"rootDir": ".",
|
|
89
|
+
"extensions": [".md", ".js", ".ts", ".tsx", ".css"],
|
|
90
|
+
"ignorePaths": ["node_modules", "build", "dist", "package-lock.json", "coverage"],
|
|
91
|
+
"lintCommand": "npm run lint"
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- `rootDir`: Specifies the root directory of your project (required).
|
|
96
|
+
- `extensions`: An array of file extensions to be considered by the tool (optional, defaults to a predefined list).
|
|
97
|
+
- `ignorePaths`: An array of paths to be ignored by the tool (optional).
|
|
98
|
+
- `lintCommand`: Specifies a lint command to be run before and after code generation (optional).
|
|
99
|
+
|
|
100
|
+
## Usage
|
|
101
|
+
|
|
102
|
+
To use GenAIcode, run the command with your desired options:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npx genaicode [options]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
For example:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
npx genaicode --allow-file-create --explicit-prompt="Add a new utility function for string manipulation"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Examples
|
|
115
|
+
|
|
116
|
+
For practical examples of using GenAIcode, visit our [Examples](examples/README.md) page.
|
|
117
|
+
|
|
118
|
+
## Demo
|
|
119
|
+
|
|
120
|
+
Here's a quick demo of GenAIcode in action:
|
|
121
|
+
|
|
122
|
+

|
|
123
|
+
|
|
124
|
+
## More Information
|
|
125
|
+
|
|
126
|
+
For in-depth details about GenAIcode's features, supported AI models, file operations, and advanced usage, please refer to our comprehensive [Design Document](docs/design/genaicode_design_doc.md).
|
|
127
|
+
|
|
128
|
+
## Feedback and Contributions
|
|
129
|
+
|
|
130
|
+
We welcome your feedback and contributions! Please feel free to open issues or submit pull requests on our GitHub repository.
|
package/bin/genaicode.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
// Add node version check
|
|
6
|
+
const pkg = require('../package.json');
|
|
7
|
+
const pleaseUpgradeNode = require('please-upgrade-node');
|
|
8
|
+
pleaseUpgradeNode(pkg);
|
|
9
|
+
|
|
10
|
+
const MONKEY_PATCH_FILE = '@google-cloud/vertexai/build/src/functions/generate_content.js';
|
|
11
|
+
const MONKEY_PATCH_TOOL_CONFIG = `// MONKEY PATCH TOOL_CONFIG`;
|
|
12
|
+
|
|
13
|
+
console.log('Apply vertex monkey patch');
|
|
14
|
+
const path = require.resolve(MONKEY_PATCH_FILE);
|
|
15
|
+
const content = fs.readFileSync(path, 'utf-8');
|
|
16
|
+
|
|
17
|
+
if (!content) {
|
|
18
|
+
console.log('Could not find file:', path);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (content.includes(MONKEY_PATCH_TOOL_CONFIG)) {
|
|
23
|
+
console.log('Patch already applied');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const newContent = content.replaceAll(
|
|
28
|
+
'data: generateContentRequest,',
|
|
29
|
+
`// MONKEY PATCH TOOL_CONFIG
|
|
30
|
+
data: {...generateContentRequest,tool_config: {function_calling_config: { mode: "ANY", allowed_function_names: request.toolConfig.functionCallingConfig.allowedFunctionNames }}},`,
|
|
31
|
+
);
|
|
32
|
+
fs.writeFileSync(path, newContent, 'utf-8');
|
|
33
|
+
console.log('Vertex monkey patch applied');
|
|
Binary file
|
|
Binary file
|
package/media/logo.png
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "genaicode",
|
|
3
|
+
"version": "0.0.31",
|
|
4
|
+
"author": "Grzegorz Tańczyk",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/gtanczyk/genaicode.git"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"generate",
|
|
11
|
+
"code",
|
|
12
|
+
"ai"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"bin": "./bin/genaicode.cjs",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"files": [
|
|
21
|
+
"bin",
|
|
22
|
+
"src",
|
|
23
|
+
"media",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"lint": "eslint . --ext js --report-unused-disable-directives --max-warnings 0",
|
|
29
|
+
"test": "vitest",
|
|
30
|
+
"coverage": "vitest run --coverage",
|
|
31
|
+
"prepare": "husky install",
|
|
32
|
+
"postinstall": "node ./bin/vertex-monkey-patch.cjs"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^7.17.0",
|
|
36
|
+
"@vitest/coverage-v8": "^2.0.5",
|
|
37
|
+
"eslint": "^8.54.0",
|
|
38
|
+
"eslint-config-prettier": "^9.0.0",
|
|
39
|
+
"eslint-plugin-import": "^2.29.0",
|
|
40
|
+
"husky": "^9.1.1",
|
|
41
|
+
"lint-staged": "^15.2.7",
|
|
42
|
+
"prettier": "^3.1.0",
|
|
43
|
+
"vitest": "^2.0.4"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@anthropic-ai/sdk": "^0.24.3",
|
|
47
|
+
"@anthropic-ai/vertex-sdk": "^0.4.1",
|
|
48
|
+
"@google-cloud/vertexai": "^1.3.0",
|
|
49
|
+
"diff": "^5.2.0",
|
|
50
|
+
"image-size": "^1.1.1",
|
|
51
|
+
"mime-types": "^2.1.35",
|
|
52
|
+
"openai": "^4.52.7",
|
|
53
|
+
"please-upgrade-node": "^3.2.0"
|
|
54
|
+
},
|
|
55
|
+
"lint-staged": {
|
|
56
|
+
"*.{js,ts,css,md}": "prettier --write"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import { printTokenUsageAndCost, processFunctionCalls } from './common.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This function generates content using the Anthropic Claude model.
|
|
6
|
+
*/
|
|
7
|
+
export async function generateContent(prompt, functionDefs, requiredFunctionName, temperature) {
|
|
8
|
+
const anthropic = new Anthropic({
|
|
9
|
+
defaultHeaders: {
|
|
10
|
+
'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15',
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const messages = prompt
|
|
15
|
+
.filter((item) => item.type !== 'systemPrompt')
|
|
16
|
+
.map((item) => {
|
|
17
|
+
if (item.type === 'user') {
|
|
18
|
+
return {
|
|
19
|
+
role: 'user',
|
|
20
|
+
content: [
|
|
21
|
+
...(item.functionResponses ?? []).map((response) => ({
|
|
22
|
+
tool_use_id: response.call_id ?? response.name,
|
|
23
|
+
content: response.content,
|
|
24
|
+
type: 'tool_result',
|
|
25
|
+
})),
|
|
26
|
+
...(item.images ?? []).map((image) => ({
|
|
27
|
+
type: 'image',
|
|
28
|
+
source: {
|
|
29
|
+
type: 'base64',
|
|
30
|
+
media_type: image.mediaType,
|
|
31
|
+
data: image.base64url,
|
|
32
|
+
},
|
|
33
|
+
})),
|
|
34
|
+
{
|
|
35
|
+
type: 'text',
|
|
36
|
+
text: item.text,
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
} else if (item.type === 'assistant') {
|
|
41
|
+
return {
|
|
42
|
+
role: 'assistant',
|
|
43
|
+
content: [
|
|
44
|
+
...(item.text ? [{ type: 'text', text: item.text }] : []),
|
|
45
|
+
...item.functionCalls.map((call) => ({
|
|
46
|
+
id: call.id ?? call.name,
|
|
47
|
+
name: call.name,
|
|
48
|
+
input: call.args ?? {},
|
|
49
|
+
type: 'tool_use',
|
|
50
|
+
})),
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const response = await anthropic.messages.create({
|
|
57
|
+
model: 'claude-3-5-sonnet-20240620',
|
|
58
|
+
system: prompt.find((item) => item.type === 'systemPrompt').systemPrompt,
|
|
59
|
+
messages,
|
|
60
|
+
tools: functionDefs.map((fd) => ({
|
|
61
|
+
name: fd.name,
|
|
62
|
+
description: fd.description,
|
|
63
|
+
input_schema: fd.parameters,
|
|
64
|
+
})),
|
|
65
|
+
tool_choice: requiredFunctionName ? { type: 'tool', name: requiredFunctionName } : { type: 'any' },
|
|
66
|
+
max_tokens: 8192,
|
|
67
|
+
temperature: temperature,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Print token usage for Anthropic
|
|
71
|
+
const usage = {
|
|
72
|
+
inputTokens: response.usage.input_tokens,
|
|
73
|
+
outputTokens: response.usage.output_tokens,
|
|
74
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
75
|
+
};
|
|
76
|
+
printTokenUsageAndCost(usage, 3 / 1000 / 1000, 15 / 1000 / 1000);
|
|
77
|
+
|
|
78
|
+
const responseMessages = response.content.filter((item) => item.type !== 'tool_use');
|
|
79
|
+
if (responseMessages.length > 0) {
|
|
80
|
+
console.log('Response messages', responseMessages);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const functionCalls = response.content
|
|
84
|
+
.filter((item) => item.type === 'tool_use')
|
|
85
|
+
.map((item) => ({
|
|
86
|
+
name: item.name,
|
|
87
|
+
args: item.input,
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
return processFunctionCalls(functionCalls);
|
|
91
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import { printTokenUsageAndCost, processFunctionCalls } from './common.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This function generates content using the OpenAI chat model.
|
|
6
|
+
*/
|
|
7
|
+
export async function generateContent(prompt, functionDefs, requiredFunctionName, temperature) {
|
|
8
|
+
const openai = new OpenAI();
|
|
9
|
+
|
|
10
|
+
const messages = prompt
|
|
11
|
+
.map((item) => {
|
|
12
|
+
if (item.type === 'systemPrompt') {
|
|
13
|
+
return {
|
|
14
|
+
role: 'system',
|
|
15
|
+
content: item.systemPrompt,
|
|
16
|
+
};
|
|
17
|
+
} else if (item.type === 'user') {
|
|
18
|
+
return [
|
|
19
|
+
...(item.functionResponses ?? []).map((response) => ({
|
|
20
|
+
role: 'tool',
|
|
21
|
+
name: response.name,
|
|
22
|
+
content: response.content ?? '',
|
|
23
|
+
tool_call_id: response.name,
|
|
24
|
+
})),
|
|
25
|
+
{
|
|
26
|
+
role: 'user',
|
|
27
|
+
content:
|
|
28
|
+
item.images?.length > 0
|
|
29
|
+
? [
|
|
30
|
+
...item.images.map((image) => ({
|
|
31
|
+
type: 'image_url',
|
|
32
|
+
image_url: {
|
|
33
|
+
url: 'data:' + image.mediaType + ';base64,' + image.base64url,
|
|
34
|
+
},
|
|
35
|
+
})),
|
|
36
|
+
{
|
|
37
|
+
type: 'text',
|
|
38
|
+
text: item.text,
|
|
39
|
+
},
|
|
40
|
+
]
|
|
41
|
+
: item.text,
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
} else if (item.type === 'assistant') {
|
|
45
|
+
return {
|
|
46
|
+
role: 'assistant',
|
|
47
|
+
...(item.text ? { content: item.text } : {}),
|
|
48
|
+
tool_calls: item.functionCalls.map((call) => ({
|
|
49
|
+
type: 'function',
|
|
50
|
+
function: { name: call.name, arguments: JSON.stringify(call.args ?? {}) },
|
|
51
|
+
id: call.name,
|
|
52
|
+
})),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
.flat();
|
|
57
|
+
|
|
58
|
+
const response = await openai.chat.completions.create({
|
|
59
|
+
model: 'gpt-4o',
|
|
60
|
+
messages,
|
|
61
|
+
tools: functionDefs.map((funDef) => ({ type: 'function', function: funDef })),
|
|
62
|
+
tool_choice: requiredFunctionName ? { type: 'function', function: { name: requiredFunctionName } } : 'required',
|
|
63
|
+
temperature: temperature,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Print token usage for chat gpt
|
|
67
|
+
const usage = {
|
|
68
|
+
inputTokens: response.usage.prompt_tokens,
|
|
69
|
+
outputTokens: response.usage.completion_tokens,
|
|
70
|
+
totalTokens: response.usage.total_tokens,
|
|
71
|
+
};
|
|
72
|
+
printTokenUsageAndCost(usage, 0.000005, 0.000015);
|
|
73
|
+
|
|
74
|
+
const responseMessage = response.choices[0].message;
|
|
75
|
+
|
|
76
|
+
if (responseMessage.content?.message) {
|
|
77
|
+
console.log('Message', responseMessage.content.message);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const toolCalls = responseMessage.tool_calls;
|
|
81
|
+
if (responseMessage.tool_calls) {
|
|
82
|
+
const functionCalls = toolCalls.map((call) => {
|
|
83
|
+
const name = call.function.name;
|
|
84
|
+
const args = JSON.parse(call.function.arguments);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
name,
|
|
88
|
+
args,
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return processFunctionCalls(functionCalls);
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error('No tool calls found in response');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { functionDefs } from './function-calling.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Common function to print token usage and estimated cost
|
|
6
|
+
* @param {Object} usage Token usage object
|
|
7
|
+
* @param {number} inputCostPerToken Cost per input token
|
|
8
|
+
* @param {number} outputCostPerToken Cost per output token
|
|
9
|
+
*/
|
|
10
|
+
export function printTokenUsageAndCost(usage, inputCostPerToken, outputCostPerToken) {
|
|
11
|
+
console.log('Token Usage:');
|
|
12
|
+
console.log(' - Input tokens: ', usage.inputTokens);
|
|
13
|
+
console.log(' - Output tokens: ', usage.outputTokens);
|
|
14
|
+
console.log(' - Total tokens: ', usage.totalTokens);
|
|
15
|
+
|
|
16
|
+
const inputCost = usage.inputTokens * inputCostPerToken;
|
|
17
|
+
const outputCost = usage.outputTokens * outputCostPerToken;
|
|
18
|
+
const totalCost = inputCost + outputCost;
|
|
19
|
+
console.log(' - Estimated cost: ', totalCost.toFixed(6), ' USD');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Common function to process function calls and explanations
|
|
24
|
+
* @param {Array} functionCalls Array of function calls
|
|
25
|
+
* @returns {Array} Processed function calls
|
|
26
|
+
*/
|
|
27
|
+
export function processFunctionCalls(functionCalls) {
|
|
28
|
+
const unknownFunctionCalls = functionCalls.filter((call) => !functionDefs.some((fd) => fd.name === call.name));
|
|
29
|
+
assert(
|
|
30
|
+
unknownFunctionCalls.length === 0,
|
|
31
|
+
'Unknown function name: ' + unknownFunctionCalls.map((call) => call.name).join(', '),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
console.log(
|
|
35
|
+
'Explanations:',
|
|
36
|
+
functionCalls.filter((fn) => fn.name === 'explanation').map((call) => call.args.text),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
return functionCalls; //.filter((fn) => fn.name !== 'explanation');
|
|
40
|
+
}
|