-
Notifications
You must be signed in to change notification settings - Fork 330
Add 'custom/chatCompletion' event in vscode extension and 'lmProvider' in lsp consuming it #9209
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
07eed16
support chatComplete call from lsp to ide
RodgeFu 0eba650
Merge remote-tracking branch 'upstream/main' into lm-from-ide
RodgeFu 8c3f13a
some further update
RodgeFu c8c30db
Merge remote-tracking branch 'upstream/main' into lm-from-ide
RodgeFu 3d0b4aa
add changelog
RodgeFu 6a961d1
expose lsp connection instead of lmprovider from compiler
RodgeFu 537bc07
update changelog
RodgeFu f5d87de
update launch
RodgeFu 801c45d
Merge branch 'main' into lm-from-ide
RodgeFu 118942e
Merge branch 'main' into lm-from-ide
RodgeFu 242fb7f
update to include options
RodgeFu b6827d4
update
RodgeFu c51c060
update 3rd
RodgeFu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| --- | ||
| changeKind: internal | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Expose LSP connection through globalThis. |
This file contains hidden or 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,7 @@ | ||
| --- | ||
| changeKind: internal | ||
| packages: | ||
| - typespec-vscode | ||
| --- | ||
|
|
||
| Add 'custom/chatComplete' custom event to expose Language Model chatComplete capability to LSP |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| export const StartFileName = "main.tsp"; | ||
| export const TspConfigFileName = "tspconfig.yaml"; | ||
| export const EmptyGuid = "00000000-0000-0000-0000-000000000000"; | ||
|
|
||
| export const ENABLE_LM_LOGGING = "ENABLE_LM_LOGGING"; |
This file contains hidden or 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,122 @@ | ||
| import { inspect } from "util"; | ||
| import { LanguageModelChat, LanguageModelChatMessage, lm } from "vscode"; | ||
| import { ENABLE_LM_LOGGING } from "../const"; | ||
| import logger, { LogItem } from "../log/logger"; | ||
| import { RetryResult, runWithRetry, runWithTimingLog } from "../utils"; | ||
|
|
||
| const lmModelCache = new Map<string, Thenable<LanguageModelChat[]>>(); | ||
| let lmParallelRequestCount = 0; | ||
|
|
||
| export interface LmChatMesage { | ||
| role: "user" | "assist"; | ||
| message: string; | ||
| } | ||
|
|
||
| export interface LmChatRequestOptions { | ||
| modelOptions?: { [name: string]: any }; | ||
| } | ||
|
|
||
| export async function sendLmChatRequest( | ||
| messages: LmChatMesage[], | ||
| modelFamily: string, | ||
| options?: LmChatRequestOptions, | ||
| /** Only for logging purpose */ | ||
| id?: string, | ||
| ): Promise<string | undefined> { | ||
| const logEnabled = process.env[ENABLE_LM_LOGGING] === "true"; | ||
| const lmLog = (item: LogItem) => { | ||
| if (logEnabled || item.level === "error" || item.level === "warning") { | ||
| logger.log( | ||
| item.level, | ||
| `[ChatComplete][#${id ?? "N/A"}] ${item.message}`, | ||
| item.details, | ||
| item.options, | ||
| ); | ||
| } | ||
| }; | ||
| if (modelFamily === undefined || modelFamily.trim() === "") { | ||
| lmLog({ level: "warning", message: `No model family provided for chat completion.` }); | ||
| return undefined; | ||
| } | ||
| if (messages === undefined || messages.length === 0) { | ||
| lmLog({ level: "warning", message: `No messages provided for chat completion.` }); | ||
| return undefined; | ||
| } | ||
|
|
||
| let models: LanguageModelChat[] | undefined; | ||
| try { | ||
| models = await runWithRetry( | ||
| "Model Selection", | ||
| async () => | ||
| await runWithTimingLog( | ||
| "Model Selection", | ||
| async () => { | ||
| let mp = lmModelCache.get(modelFamily); | ||
| if (!mp) { | ||
| mp = lm.selectChatModels({ family: modelFamily }); | ||
| lmModelCache.set(modelFamily, mp); | ||
| } | ||
| const m = await mp; | ||
| if (!m || m.length === 0) { | ||
| lmModelCache.delete(modelFamily); | ||
| return RetryResult.Failed; | ||
| } | ||
| return m; | ||
| }, | ||
| lmLog, | ||
| ), | ||
| lmLog, | ||
| ); | ||
| } catch (e) { | ||
| lmLog({ | ||
| level: "error", | ||
| message: `Error when selecting chat models for family ${modelFamily}`, | ||
| details: [e], | ||
| }); | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| lmParallelRequestCount++; | ||
| return await runWithTimingLog( | ||
| "Send request to LM", | ||
| async () => { | ||
| lmLog({ level: "debug", message: `LM parallelism increased to ${lmParallelRequestCount}` }); | ||
| const selectedModel = models[0]; | ||
| lmLog({ | ||
| level: "debug", | ||
| message: `Requested model family: ${modelFamily}, selected: ${selectedModel.family}`, | ||
| }); | ||
|
|
||
| const response = await selectedModel.sendRequest( | ||
| messages.map((m) => { | ||
| if (m.role === "user") { | ||
| return LanguageModelChatMessage.User(m.message); | ||
| } else if (m.role === "assist") { | ||
| return LanguageModelChatMessage.Assistant(m.message); | ||
| } else { | ||
| logger.debug(`Unknown chat message role: ${m.role}. Default to use User role.`); | ||
| return LanguageModelChatMessage.User(m.message); | ||
| } | ||
| }), | ||
| options, | ||
| ); | ||
| let fullResponse = ""; | ||
| for await (const chunk of response.text) { | ||
| fullResponse += chunk; | ||
| } | ||
| return fullResponse; | ||
| }, | ||
| lmLog, | ||
| ); | ||
| } catch (e) { | ||
| lmLog({ | ||
| level: "error", | ||
| message: `Error when sending chat completion request to model ${models[0].name}. error: ${inspect(e)}`, | ||
| }); | ||
| return undefined; | ||
| } finally { | ||
| lmParallelRequestCount--; | ||
| lmLog({ level: "debug", message: `LM parallelism reduced to ${lmParallelRequestCount}` }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.