Skip to content

Commit

Permalink
Add code completer based on Anthropic Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Dec 17, 2024
1 parent b2e46db commit 1da9ec1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/completion-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
IInlineCompletionContext,
IInlineCompletionProvider
} from '@jupyterlab/completer';
import { LLM } from '@langchain/core/language_models/llms';
import { BaseLanguageModel } from '@langchain/core/language_models/base';
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';

import { getCompleter, IBaseCompleter, BaseCompleter } from './llm-models';
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';

/**
* The generic completion provider to register to the completion provider manager.
Expand Down Expand Up @@ -57,7 +57,7 @@ export class CompletionProvider implements IInlineCompletionProvider {
/**
* Get the LLM completer.
*/
get llmCompleter(): LLM | null {
get llmCompleter(): BaseLanguageModel | null {
return this._completer?.provider || null;
}

Expand Down
67 changes: 67 additions & 0 deletions src/llm-models/anthropic-completer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
CompletionHandler,
IInlineCompletionContext
} from '@jupyterlab/completer';
import { ChatAnthropic } from '@langchain/anthropic';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { AIMessage, SystemMessage } from '@langchain/core/messages';

import { BaseCompleter, IBaseCompleter } from './base-completer';

export class AnthropicCompleter implements IBaseCompleter {
constructor(options: BaseCompleter.IOptions) {
this._anthropicProvider = new ChatAnthropic({ ...options.settings });
}

get provider(): BaseChatModel {
return this._anthropicProvider;
}

async fetch(
request: CompletionHandler.IRequest,
context: IInlineCompletionContext
) {
const { text, offset: cursorOffset } = request;
const prompt = text.slice(0, cursorOffset);

// Anthropic does not allow whitespace at the end of the AIMessage
const trimmedPrompt = prompt.trim();

const messages = [
new SystemMessage(
'You ara a code completion bot which complete the following code from a Jupyter Notebook cell.'
),
new AIMessage(trimmedPrompt)
];

try {
const response = await this._anthropicProvider.invoke(messages);
console.log('CONTENT', response.content);
const items = [];

// Anthropic can return string or complex content, a list of string/images/other.
if (typeof response.content === 'string') {
items.push({
insertText: response.content,
filterText: prompt.substring(trimmedPrompt.length)
});
} else {
response.content.forEach(content => {
if (content.type !== 'text') {
return;
}
items.push({
insertText: content.text,
filterText: prompt.substring(trimmedPrompt.length)
});
});
}
return { items };
} catch (error) {
console.error('Error fetching completions', error);
return { items: [] };
}
}

private _anthropicProvider: ChatAnthropic;
}
4 changes: 2 additions & 2 deletions src/llm-models/base-completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
CompletionHandler,
IInlineCompletionContext
} from '@jupyterlab/completer';
import { LLM } from '@langchain/core/language_models/llms';
import { BaseLanguageModel } from '@langchain/core/language_models/base';
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';

export interface IBaseCompleter {
/**
* The LLM completer.
*/
provider: LLM;
provider: BaseLanguageModel;

/**
* The function to fetch a new completion.
Expand Down
3 changes: 3 additions & 0 deletions src/llm-models/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatAnthropic } from '@langchain/anthropic';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { ChatMistralAI } from '@langchain/mistralai';
import { IBaseCompleter } from './base-completer';
import { AnthropicCompleter } from './anthropic-completer';
import { CodestralCompleter } from './codestral-completer';
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';

Expand All @@ -14,6 +15,8 @@ export function getCompleter(
): IBaseCompleter | null {
if (name === 'MistralAI') {
return new CodestralCompleter({ settings });
} else if (name === 'Anthropic') {
return new AnthropicCompleter({ settings });
}
return null;
}
Expand Down

0 comments on commit 1da9ec1

Please sign in to comment.