|
1 |
| -# Git Repository Template |
| 1 | +# AI21 API Client |
2 | 2 |
|
3 |
| -[Template](https://github.com/DynamicYield/template) is a [*Git repository template*](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template) for bootstrapping your repositories. |
| 3 | +The AI21 API Client is a TypeScript library that provides a convenient interface for interacting with the AI21 API. It abstracts away the low-level details of making API requests and handling responses, allowing developers to focus on building their applications. |
4 | 4 |
|
5 |
| -Bundled support for: |
| 5 | +## Installation |
6 | 6 |
|
7 |
| -- [Code Owners](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#about-code-owners) — Automatically requested for review when someone opens a pull request that modifies code that they own as defined in [`.github/CODEOWNERS`](.github/CODEOWNERS) |
| 7 | +You can install the AI21 API Client using npm or yarn: |
8 | 8 |
|
9 |
| - - The first thing you should do, is **set a [team](https://github.com/AI21/template/blob/main/.github/CODEOWNERS) as a code owner for the `.github/settings.yml`**. |
10 |
| - |
11 |
| -- [ProBot Settings](https://github.com/probot/settings) — Synchronize repository settings defined in [`.github/settings.yml`](.github/settings.yml) to GitHub, enabling Pull Requests for repository settings |
12 |
| - |
13 |
| - When adding/removing/modifying jobs within workflows, you might need to tweak the *required status checks* in this file; the required status checks must pass before you can merge your branch into the protected branch. |
| 9 | +```bash |
| 10 | +npm install ai21 |
| 11 | +``` |
14 | 12 |
|
15 |
| -- [ProBot Stale](https://github.com/probot/stale/) — Closes abandoned Issues and Pull Requests after a period of inactivity as defined in [`.github/stale.yml`](.github/stale.yml) |
16 |
| -- [DependaBot](https://docs.github.com/en/code-security/supply-chain-security/keeping-your-dependencies-updated-automatically/about-dependabot-version-updates) — Alerts on security vulnerabilities within the repository's dependencies, and updates the dependencies automatically as defined in [`.github/dependabot.yml`](.github/dependabot.yml) |
17 |
| -- [RenovateBot](https://github.com/renovatebot/renovate) — Universal dependency update tool as defined in [`.github/renovate.json`](.github/renovate.json) ([application dashboard](https://app.renovatebot.com)) |
18 |
| -- [Pre-commit](https://pre-commit.com/) — Managing and maintaining multi-language pre-commit hooks as defined in [`.pre-commit-config.yaml`](.pre-commit-config.yaml) |
| 13 | +or |
19 | 14 |
|
20 |
| - - Leverage `pre-commit` to [install the Git hooks](https://pre-commit.com/#pre-commit-install) |
| 15 | +```bash |
| 16 | +yarn add ai21 |
| 17 | +``` |
21 | 18 |
|
22 |
| - ```shell |
23 |
| - pre-commit install --install-hooks -t pre-commit -t commit-msg |
24 |
| - ``` |
| 19 | +## Usage |
25 | 20 |
|
26 |
| - - You can check which files `pre-commit` works on by running |
| 21 | +To use the AI21 API Client, you'll need to have an API key. You can obtain an API key by signing up for an account on the AI21 website. |
27 | 22 |
|
28 |
| - ```shell |
29 |
| - pre-commit run list-files --hook-stage manual --verbose |
30 |
| - ``` |
| 23 | +Here's an example of how to use the `AI21` class to interact with the API: |
31 | 24 |
|
32 |
| -- [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) — An easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. Such as generating a [changelog](https://keepachangelog.com/) |
| 25 | +```typescript |
| 26 | +import { AI21 } from 'ai21'; |
33 | 27 |
|
34 |
| - ```shell |
35 |
| - git cz changelog |
36 |
| - ``` |
| 28 | +const client = new AI21({ |
| 29 | + apiKey: process.env.AI21_API_KEY, // or pass it in directly |
| 30 | +}); |
37 | 31 |
|
38 |
| ---- |
| 32 | +const response = await client.chat.completions.create({ |
| 33 | + model: 'jamba-1.5-mini', |
| 34 | + messages: [{ role: 'user', content: 'Hello, how are you? tell me a 100 line story about a cat named "Fluffy"' }], |
| 35 | +}); |
39 | 36 |
|
40 |
| -## Automation Features |
| 37 | +console.log(response.data); |
| 38 | +``` |
41 | 39 |
|
42 |
| -We **strongly recommend** to enable the following features manually |
| 40 | +### Streaming Responses |
43 | 41 |
|
44 |
| -### Code review assignment |
| 42 | +The client supports streaming responses for real-time processing. Here are examples using different approaches: |
45 | 43 |
|
46 |
| -[Code review assignments clearly indicate which members of a team are expected to submit a review for a pull request](https://docs.github.com/en/organizations/organizing-members-into-teams/managing-code-review-assignment-for-your-team). |
| 44 | +#### Using Async Iterator |
47 | 45 |
|
48 |
| -### Scheduled reminders |
| 46 | +```typescript |
| 47 | +const stream = await ai21.chat.completions.create({ |
| 48 | + model: 'jamba-1.5-mini', |
| 49 | + messages: [{ role: 'user', content: 'Write a story about a space cat' }], |
| 50 | + stream: true, |
| 51 | +}); |
49 | 52 |
|
50 |
| -You can get reminders in Slack when your [team has pull requests waiting for review](https://docs.github.com/en/organizations/organizing-members-into-teams/managing-scheduled-reminders-for-your-team#creating-a-scheduled-reminder-for-a-team) or for [your user with real-time alerts](https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/managing-your-membership-in-organizations/managing-your-scheduled-reminders). |
| 53 | +for await (const chunk of stream) { |
| 54 | + console.log(chunk.choices[0]?.delta?.content || ''); |
| 55 | +} |
| 56 | +``` |
51 | 57 |
|
52 |
| -## Prerequisites |
| 58 | +The `AI21` class provides a `chat` property that gives you access to the Chat API. You can use this to generate text, complete prompts, and more. |
53 | 59 |
|
54 |
| -We are using a collection of tools. In order to work with this repository please [configure your Mac](https://github.com/AI21/dev-envs#getting-started) |
55 | 60 |
|
56 |
| -## Quick Start |
| 61 | +## Configuration |
57 | 62 |
|
58 |
| -Run [`./bootstrap.sh`](./bootstrap.sh) and follow the interactive on-screen instructions. For more information, Run `./bootstrap.sh --help`. |
| 63 | +The `AI21` class accepts several configuration options, which you can pass in when creating a new instance: |
59 | 64 |
|
60 |
| -This will install pre-commit hooks, modify relevant files, deletes itself and open a draft pull-request. |
| 65 | +- `baseURL`: The base URL for the API endpoint (default: `https://api.ai21.com/studio/v1`) |
| 66 | +- `apiKey`: Your AI21 API key |
| 67 | +- `maxRetries`: The maximum number of retries for failed requests (default: `3`) |
| 68 | +- `timeout`: The request timeout in seconds |
61 | 69 |
|
62 |
| -## Syncing with the Template repository |
| 70 | +## API Reference |
63 | 71 |
|
64 |
| -To keep your repository up-to-date with the template git repository, execute the following from within your repository's root directory |
| 72 | +For detailed information about the available methods and their parameters, please refer to the [API reference documentation](https://docs.ai21.com/docs). |
65 | 73 |
|
66 |
| -```shell |
67 |
| -git clone [email protected]:AI21/template.git ../template |
68 |
| -git checkout -b template-sync |
69 |
| -rsync -ax --exclude .git --exclude README.md --exclude CHANGELOG.md --exclude bootstrap.sh ../template/ . |
70 |
| -``` |
| 74 | +## Contributing |
71 | 75 |
|
72 |
| -Then do cherry-picking for the changes which you would like to merge. |
| 76 | +If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/ai21-labs/api-client). |
73 | 77 |
|
74 |
| ---- |
| 78 | +## License |
75 | 79 |
|
76 |
| -## Modify this README to suit the project |
| 80 | +This library is licensed under the [MIT License](LICENSE). |
0 commit comments