-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anthropic (Claude) provider #22
Changes from all commits
7ae0a5e
ea31382
80c60e2
7f3781a
317fbbb
dc8e602
b74d481
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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 = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if this list of messages should be recreated each time, or it should be persisted per provider? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some other open questions: should the list of messages be shared across providers? Or should it be reset when a user switches to another provider? Maybe a reset would be fine to keep things tidy. Also users probably wouldn't change providers often in practice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #29 |
||
new SystemMessage( | ||
'You are a code-completion AI completing the following code from a Jupyter Notebook cell.' | ||
), | ||
new AIMessage(trimmedPrompt) | ||
]; | ||
|
||
try { | ||
const response = await this._anthropicProvider.invoke(messages); | ||
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 | ||
}); | ||
} 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably fine to have exclusion clauses like this for now as we figure things out with several providers 👍
Also found that some props had to be excluded over in #27.