-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code completer based on Anthropic Chat
- Loading branch information
Showing
4 changed files
with
75 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters