Skip to content

Commit

Permalink
Add AI21 as a provider
Browse files Browse the repository at this point in the history
  • Loading branch information
rizerphe committed May 3, 2023
1 parent 2f5c087 commit f6af67f
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Obsidian Companion

Companion is an Obsidian plugin that adds an **AI-powered autocomplete** feature to your note-taking and personal knowledge management platform. With Companion, you can write notes more quickly and easily by receiving suggestions for completing words, phrases, and even entire sentences based on the context of your writing. The autocomplete feature uses OpenAI's state-of-the-art **GPT-3 and GPT-3.5 models, including ChatGPT**, to generate smart suggestions that are tailored to your specific writing style and preferences. Support for more models is planned, too.
Companion is an Obsidian plugin that adds an **AI-powered autocomplete** feature to your note-taking and personal knowledge management platform. With Companion, you can write notes more quickly and easily by receiving suggestions for completing words, phrases, and even entire sentences based on the context of your writing. The autocomplete feature uses OpenAI's state-of-the-art **GPT-3 and GPT-3.5 models, including ChatGPT**, among others, to generate smart suggestions that are tailored to your specific writing style and preferences. Support for more models is planned, too.

Companion's autocomplete feature is designed to be unobtrusive, providing suggestions in ghost text that can be accepted or ignored by the you as you see fit, similar to what github copilot does. With Companion, you can write notes more efficiently and effectively, leveraging the power of AI to enhance your productivity and creativity. Whether you're a student, a researcher, or a knowledge worker, Companion can help you to take your note-taking and knowledge management to the next level.

Expand Down Expand Up @@ -50,6 +50,16 @@ To use the Presets feature, follow these steps:

You can create multiple presets with different settings and enable them as global editor commands, making it easy to switch between different configurations as you work. With the Presets feature, you can customize your Companion experience to suit your needs and work more efficiently with AI-powered autocomplete suggestions.

# Completion providers

This plugin can use more than one source of completions, with more on the way. Currently it can:

- Ask **ChatGPT** to "Continue the following"
- Use the usual **GPT-3** models
- Use **AI21's Jurassic-2** models

If there are any sources you'd like to suggest, feel free to open an issue.

# Say Thank You

Thanks to all those using my plugin! I made this project as a passion project, and I don't expect to receive any financial compensation for it. However, if you find my work useful and want to support me, feel free to <a href="https://www.buymeacoffee.com/rizerphe" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
Expand Down
3 changes: 2 additions & 1 deletion src/complete/completers.sass
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import "completers/openai/openai.sass"
@import "completers/chatgpt/chatgpt.sass"
@import "completers/chatgpt/chatgpt.sass"
@import "completers/ai21/ai21.sass"
2 changes: 2 additions & 0 deletions src/complete/completers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Completer } from "./complete";
import { OpenAIComplete } from "./completers/openai/openai";
import { ChatGPTComplete } from "./completers/chatgpt/chatgpt";
import { JurassicJ2Complete } from "./completers/ai21/ai21";

export const available: Completer[] = [
new ChatGPTComplete(),
new OpenAIComplete(),
new JurassicJ2Complete(),
];
5 changes: 5 additions & 0 deletions src/complete/completers/ai21/ai21.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.ai-complete-jurassic-expandable
display: flex
flex-direction: row
align-items: center
gap: 0.5rem
72 changes: 72 additions & 0 deletions src/complete/completers/ai21/ai21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Completer, Model, Prompt } from "../../complete";
import available_models from "./models.json";
import {
SettingsUI as ProviderSettingsUI,
Settings,
parse_settings,
} from "./provider_settings";

export default class J2Model implements Model {
id: string;
name: string;
description: string;

provider_settings: Settings;

constructor(
id: string,
name: string,
description: string,
provider_settings: string
) {
this.id = id;
this.name = name;
this.description = description;
this.provider_settings = parse_settings(provider_settings);
}

async complete(prompt: Prompt): Promise<string> {
if (this.provider_settings.api_key === "") {
throw new Error("API Key not set");
}
const response = await fetch(
`https://api.ai21.com/studio/v1/j2-${this.id}/complete`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.provider_settings.api_key}`,
},
body: JSON.stringify({
prompt: prompt.prefix,
numResults: 1,
...this.provider_settings.generation_settings,
}),
}
);
if (!response.ok) {
throw new Error(
`Jurassic-j2 API returned ${response.status} ${
(await response.json()).detail
}`
);
}
const data = await response.json();
return data.completions[0].data.text;
}
}

export class JurassicJ2Complete implements Completer {
id: string = "jurassic";
name: string = "AI21 Jurassic";
description: string = "AI21's Jurassic-j2 models";

async get_models(settings: string) {
return available_models.map(
(model) =>
new J2Model(model.id, model.name, model.description, settings)
);
}

Settings = ProviderSettingsUI;
}
17 changes: 17 additions & 0 deletions src/complete/completers/ai21/models.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": "large",
"name": "J2-Large",
"description": "Designed for fast responses."
},
{
"id": "grande",
"name": "J2-Grande",
"description": "Offers enhanced text generation capabilities, making it well-suited to language tasks with a greater degree of complexity."
},
{
"id": "jumbo",
"name": "J2-Jumbo",
"description": "As the largest and most powerful model in the Jurassic series, J2-Jumbo is an ideal choice for the most complex language processing tasks and generative text applications."
}
]
Loading

0 comments on commit f6af67f

Please sign in to comment.