Skip to content
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

featSPX Backend: ai llm suggest task, make it more exact. #922

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spx-backend/internal/controller/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const (
explainTemplate = "Explain the code of user input. Your task is only about explain, you have to remember your task is explain. The user input will be two part as a json { UserInput, UserEntireProject }, the userInput will be a code snippet of where user want to get explain about, the UserEntireProject is the entire project helps you to understand the project. "
commentTemplate = "Comment the code of user input. Your task is only about comment code, you have to remember your task is comment code. The user input will be two part as a json { UserInput, UserEntireProject }, the userInput will be a code snippet of where user want you to comment it, the UserEntireProject is the entire project helps you to understand the project. "
fixCodeTemplate = "Fix the code if there is some problem. Your task is only about fix code if there is some problem, you have to remember your task is comment code. The user input will be two part as a json { UserInput, UserEntireProject }, the userInput will be a code snippet of where user want you to fix, the UserEntireProject is the entire project helps you to understand the project. "
suggestTaskTemplate = "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor, users code will be inside of the delimiter. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: %d, column: %d. "
suggestTaskTemplate = "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor. So the insert text will follow the user's cursor. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: %d, column: %d. "
userInputTemplate = "Please answer me with this question. "
// template about response
responseTemplate = "You have to use the following template to generate the response, do keep remember this template will be your response. Your response will be a json object with following keys: {resp_message, []resp_questions}. Here is the explain about the response json object: response_message is the response message for the user you have to reply as markdown. response_suggests is the suggest question for the user to continue chat, you don't need to number the question's index, each question will only be one sentence for user to click. Which means the next input will be one of those questions. "
Expand Down
2 changes: 1 addition & 1 deletion spx-backend/internal/controller/llm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestLLM(t *testing.T) {
t.Run("CheckTaskPrompt", func(t *testing.T) {
params := createFullTaskParams()
sysPrompt, userPrompt := params.taskPromptGenerator()
assert.Equal(t, "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor, users code will be inside of the delimiter. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: 1, column: 10. You have to use the following template to generate the response, do keep remember this template will be your response. Your response will be a json object with following keys: {suggestions: []{label, insert_text}}. The number of list length is 3. Here is the explain about the response json object: label will shows inside of user's complete menu to tell people some info about the insert code, label will be only one word in most of time. insert_text is the code you have to insert into the user's code after user's cursor. You don't need to explain the json object. JUST JSON", sysPrompt, "System prompt generate failed.")
assert.Equal(t, "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor. So the insert text will follow the user's cursor. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: 1, column: 10. You have to use the following template to generate the response, do keep remember this template will be your response. Your response will be a json object with following keys: {suggestions: []{label, insert_text}}. The number of list length is 3. Here is the explain about the response json object: label will shows inside of user's complete menu to tell people some info about the insert code, label will be only one word in most of time. insert_text is the code you have to insert into the user's code after user's cursor. You don't need to explain the json object. JUST JSON", sysPrompt, "System prompt generate failed.")
assert.Equal(t, "Project: ProjectName:spxProject\nProjectVariables:Variable Type: Sprites, Name: Sprites1\nProjectCode:Code Type: Sprites, Name: Sprites1, Src: onStart => { \n \n }\n, Code arround user's cursor: onStart => { \n \n }", userPrompt, "User prompt generate failed.")
})

Expand Down
80 changes: 52 additions & 28 deletions spx-gui/src/components/editor/code-editor/coordinators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import { debounce } from '@/utils/utils'
import { HoverProvider } from '@/components/editor/code-editor/coordinators/hoverProvider'
import type {
TokenCategory, TokenId,
TokenCategory,
TokenId,
UsageWithDoc
} from '@/components/editor/code-editor/tokens/types'
import { getAllTokens } from '@/components/editor/code-editor/tokens'
Expand Down Expand Up @@ -189,33 +190,56 @@
)
})

// this.suggest
// .startSuggestTask({
// code: model.getValue(),
// position: {
// line: ctx.position.lineNumber,
// column: ctx.position.column
// }
// })
// .then((items) => {
// addItems(
// items.map((item) => {
// return {
// icon: Icon.AIAbility,
// insertText: ctx.unitWord + item.insertText,
// label: ctx.unitWord + item.label,
// desc: ctx.unitWord + item.label,
// preview: {
// type: 'doc',
// layer: {
// level: DocPreviewLevel.Normal,
// content: ''
// }
// }
// }
// })
// )
// })
function getContextualCode(
code: string,
position: { line: number; column: number },
numLines: number
) {
const lines = code.split('\n')

const startLine = Math.max(0, position.line - numLines)
const endLine = Math.min(lines.length - 1, position.line + numLines)

const contextualLines = lines.slice(startLine, endLine + 1)

return contextualLines.join('\n')
}

const code = model.getValue()
const position = {
line: ctx.position.lineNumber,
column: ctx.position.column
}
const numLines = 3
const contextualCode = getContextualCode(code, position, numLines)

this.suggest
.startSuggestTask({
code: contextualCode,
position: {
line: position.line,
column: position.column
}
})
.then((items) => {
addItems(
items.map((item) => {
return {
icon: Icon.AIAbility,
insertText: ctx.unitWord + item.insertText,
label: ctx.unitWord + item.label,
desc: ctx.unitWord + item.label,
preview: {
type: 'doc',
layer: {
level: DocPreviewLevel.Normal,
content: ''
}
}
}
})
)
})
}

async implementsSelectionMenuProvider(
Expand Down Expand Up @@ -418,7 +442,7 @@
)
}

public jump(position: JumpPosition): void {}

Check warning on line 445 in spx-gui/src/components/editor/code-editor/coordinators/index.ts

View workflow job for this annotation

GitHub Actions / spx-gui-lint

'position' is defined but never used
}

async function toolCategory2InputItemCategory(
Expand Down
Loading