An open API service indexing awesome lists of open source software.

https://github.com/madeindjs/mistral-js-tool-calls

Run JS function as Mistral Tools calls
https://github.com/madeindjs/mistral-js-tool-calls

jsdoc llm mistralai

Last synced: 6 months ago
JSON representation

Run JS function as Mistral Tools calls

Awesome Lists containing this project

README

          

# Run JS function as Mistral Tools calls

This is a "fun" experiment to turn JavaScript [JSdoc](https://jsdoc.app/) comments as [Function calling](https://docs.mistral.ai/capabilities/function_calling/) using the Mistral client.

It's really handy to quickly prototype or automatise something like:

- calling an external API
- execute a script on the machine
- etc..

## Usage

```sh
npm install mistral-js-tool-calls
```

Let's say you have a simple script which exposes two functions:

```js
import { hostname } from "node:os";
import process from "node:process";

/**
* @description get the hostname
* @returns {string} the hostname
*/
export function getHostname() {
return hostname();
}

/**
* @description Get the environment variable
* @param {string} name the name of the variable key
* @returns {string} the environment variable value
*/
export function getEnvVariable(name) {
return process.env[name] ?? "not found";
}
```

> [!NOTE]
> The important part is the JSdoc comments. This library will extract them.

This library allow you to starts an interactive chat using the command

```sh
MISTRAL_API_KEY= mistral-js-tool-calls ./sample/os.js
```

```
>>> what is the environment variable named `HOME` and my hostname ?

The environment variable named `HOME` is `/home/alexandre` and your hostname is `thinkpad-t14`.
```

Or use it as library

```js
import { Mistral } from "@mistralai/mistralai";
import { runInteractiveChat } from "mistral-js-tool-calls";

const client = new Mistral({ apiKey: "" });

const stream = await chatWithToolCall("./your-script.js", client, {
model: "open-mistral-7b",
toolChoice: "auto",
temperature: 0.2,
messages: [
{
role: "user",
content:
"what is the environment variable named `HOME` and my hostname ?",
},
],
});
for await (const chunk of stream) process.stdout.write(chunk);
```

## Todos

- [ ] use other providers
- [ ] checks the order of parameters
- [ ] throw if function uses a non-serializable object (function, class, etc...)
- [ ] solve issue with entrypoint from CLI not starting by `./`