Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to config the api version #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,17 @@ const gemini = new Gemini(API_KEY, {

<h2 align="center" id="contributors">Contributors</h2>
<p align="center">A special shoutout to developers of and contributors to the <a href="https://github.com/EvanZhouDev/bard-ai"><code>bard-ai</code></a> and <a href="https://github.com/EvanZhouDev/palm-api"><code>palm-api</code></a> libraries. Gemini AI's interface is heavily based on what we have developed on these two projects.</p>

### I Want more control over the API version

By default the project uses the `v1beta` API version to enable the most features and backwards compatibility. You can read more about it [here](https://ai.google.dev/docs/api_versions).

If you feel the need to use a different version than the default you can pass it as an option on gemini initialization:

```javascript
import Gemini from "gemini-ai";

const gemini = new Gemini(API_KEY, {
apiVersion: "v1",
});
```
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const answerPairToParameter = (message) => {
export default class Gemini {
#fetch;
#dispatcher;
#apiVersion;

static JSON = "json";
static TEXT = "markdown";
Expand All @@ -61,6 +62,7 @@ export default class Gemini {
const config = this.#parseConfig(rawConfig, {
fetch: defaultFetch,
dispatcher: undefined,
apiVersion: 'v1beta'
});

if (!config.fetch)
Expand All @@ -71,6 +73,7 @@ export default class Gemini {
this.#fetch = config.fetch;
this.key = key;
this.#dispatcher = config.dispatcher;
this.#apiVersion = config.apiVersion;
}

#parseConfig(raw = {}, defaults = {}) {
Expand Down Expand Up @@ -110,7 +113,7 @@ export default class Gemini {
};

const response = await this.#fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${model}:${command}?key=${this.key}`,
`https://generativelanguage.googleapis.com/${this.#apiVersion}/models/${model}:${command}?key=${this.key}`,
opts,
);

Expand Down
9 changes: 9 additions & 0 deletions test/fullCoverage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,12 @@ test("Gemini.createChat() with JSON", async () => {
}),
).toBe(generateContentResponse);
});

test("Gemini Create with correct version", async () => {
const fetchSpy = vi.spyOn(global, 'fetch')
fetch.mockReturnValueOnce(createFetchResponse(generateContentResponse));

const gemini = new Gemini(API_KEY, { apiVersion: "v1" });
expect(await gemini.ask("Hello!")).toBe("Hi!");
expect(fetchSpy).toHaveBeenCalledWith('https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=demo-key', expect.any(Object));
})