gemini-wrapper-ashes 1.0.0
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/README.md +71 -0
- package/dist/GeminiClient.d.ts +20 -0
- package/dist/GeminiClient.js +51 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +8 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +1 -0
- package/package.json +21 -0
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# gemini-wrapper-ashes
|
|
2
|
+
|
|
3
|
+
A lightweight, TypeScript-first wrapper around the Google Gemini API that simplifies model configuration, system instructions, and response handling.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Node.js 18 or later
|
|
10
|
+
- A valid Google Gemini API key
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
npm install gemini-wrapper-ashes
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## User Manual
|
|
21
|
+
|
|
22
|
+
### Create a Client
|
|
23
|
+
|
|
24
|
+
import { GeminiClient } from "gemini-wrapper-ashes";
|
|
25
|
+
|
|
26
|
+
const gemini = new GeminiClient({
|
|
27
|
+
apiKey: process.env.GEMINI_API_KEY,
|
|
28
|
+
model: "gemini-1.5-flash",
|
|
29
|
+
instruction: "You are a senior software engineer"
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
### Ask a Question
|
|
35
|
+
|
|
36
|
+
const response = await gemini.ask("Explain closures in JavaScript");
|
|
37
|
+
console.log(response);
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
### Change System Instruction
|
|
42
|
+
|
|
43
|
+
gemini.setInstruction("You are a strict technical interviewer");
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
### Change Model
|
|
48
|
+
|
|
49
|
+
gemini.setModel("gemini-1.5-pro");
|
|
50
|
+
|
|
51
|
+
Any valid Gemini model string is supported. Model validation is delegated to the Gemini API.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
### Error Handling
|
|
56
|
+
|
|
57
|
+
import { GeminiError } from "gemini-wrapper-ashes";
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
await gemini.ask("Hello");
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof GeminiError) {
|
|
63
|
+
console.error(error.message);
|
|
64
|
+
console.error(error.code);
|
|
65
|
+
console.error(error.status);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { GeminiClientConfig } from "./types";
|
|
2
|
+
export declare class GeminiClient {
|
|
3
|
+
private genAI;
|
|
4
|
+
private model;
|
|
5
|
+
private modelName;
|
|
6
|
+
private instruction;
|
|
7
|
+
constructor(config: GeminiClientConfig);
|
|
8
|
+
/**
|
|
9
|
+
* Update system instruction
|
|
10
|
+
*/
|
|
11
|
+
setInstruction(instruction: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* Update Gemini model (ANY model allowed)
|
|
14
|
+
*/
|
|
15
|
+
setModel(modelName: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Ask Gemini a question
|
|
18
|
+
*/
|
|
19
|
+
ask(prompt: string): Promise<string>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
2
|
+
import { GeminiError } from "./errors";
|
|
3
|
+
export class GeminiClient {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
if (!config.apiKey) {
|
|
6
|
+
throw new GeminiError("Gemini API key is required");
|
|
7
|
+
}
|
|
8
|
+
this.genAI = new GoogleGenerativeAI(config.apiKey);
|
|
9
|
+
this.modelName = config.model ?? "gemini-1.5-flash";
|
|
10
|
+
this.instruction = config.instruction ?? "";
|
|
11
|
+
this.model = this.genAI.getGenerativeModel({
|
|
12
|
+
model: this.modelName,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Update system instruction
|
|
17
|
+
*/
|
|
18
|
+
setInstruction(instruction) {
|
|
19
|
+
this.instruction = instruction;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Update Gemini model (ANY model allowed)
|
|
23
|
+
*/
|
|
24
|
+
setModel(modelName) {
|
|
25
|
+
if (!modelName || !modelName.trim()) {
|
|
26
|
+
throw new GeminiError("Model name cannot be empty");
|
|
27
|
+
}
|
|
28
|
+
this.modelName = modelName;
|
|
29
|
+
this.model = this.genAI.getGenerativeModel({
|
|
30
|
+
model: modelName,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Ask Gemini a question
|
|
35
|
+
*/
|
|
36
|
+
async ask(prompt) {
|
|
37
|
+
if (!prompt || !prompt.trim()) {
|
|
38
|
+
throw new GeminiError("Prompt cannot be empty");
|
|
39
|
+
}
|
|
40
|
+
const finalPrompt = this.instruction
|
|
41
|
+
? `SYSTEM:\n${this.instruction}\n\nUSER:\n${prompt}`
|
|
42
|
+
: prompt;
|
|
43
|
+
try {
|
|
44
|
+
const result = await this.model.generateContent(finalPrompt);
|
|
45
|
+
return result.response.text().trim();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new GeminiError(error?.message || "Gemini request failed", error?.code, error?.status || error?.response?.status);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gemini-wrapper-ashes",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple and clean wrapper around Google Gemini API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"keywords": ["gemini", "ai", "llm", "google", "wrapper"],
|
|
13
|
+
"author": "Ashes",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@google/generative-ai": "^0.21.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"typescript": "^5.4.0"
|
|
20
|
+
}
|
|
21
|
+
}
|