Skip to content

Commit

Permalink
fix allways supply file heading without file context
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Apr 26, 2024
1 parent 9714246 commit fd3655e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Twinny supports OpenAI API-compliant providers.

## Workspace Context

Enable `useFileContext` in settings to improve completion quality by tracking sessions and file access patterns. This is off by default to ensure performance.
Enable `fileContextEnabled` in settings to improve completion quality by tracking sessions and file access patterns. This is off by default to ensure performance.

## Known Issues

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"default": 30,
"description": "Maximum number of lines to use for multi line completions. Applicable only when multilineCompletionsEnabled is enabled."
},
"twinny.useFileContext": {
"twinny.fileContextEnabled": {
"order": 8,
"type": "boolean",
"default": false,
Expand Down
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export interface FimPromptTemplate {
context: string
header: string
prefixSuffix: PrefixSuffix
useFileContext: boolean
fileContextEnabled: boolean
language?: string
}

Expand Down
73 changes: 46 additions & 27 deletions src/extension/fim-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,87 @@ import {
import { supportedLanguages } from '../common/languages'
import { FimPromptTemplate } from '../common/types'

const getFileContext = (
fileContextEnabled: boolean,
context: string,
language: string | undefined,
header: string
) => {
const languageId =
supportedLanguages[language as keyof typeof supportedLanguages]
const fileContext = fileContextEnabled
? `${languageId?.syntaxComments?.start || ''}${context}${
languageId?.syntaxComments?.end || ''
}`
: ''
return { heading: header ?? '', fileContext }
}

export const getFimPromptTemplateLLama = ({
context,
header,
useFileContext,
fileContextEnabled,
prefixSuffix,
language
}: FimPromptTemplate) => {
const { prefix, suffix } = prefixSuffix
const languageId =
supportedLanguages[language as keyof typeof supportedLanguages]
const fileContext = useFileContext
? `${languageId?.syntaxComments?.start || ''}${context}${
languageId?.syntaxComments?.end || ''
}`
: ''
const heading = useFileContext && header ? header : ''
const { fileContext, heading } = getFileContext(
fileContextEnabled,
context,
language,
header
)
return `<PRE>${fileContext} \n${heading}${prefix} <SUF> ${suffix} <MID>`
}

export const getDefaultFimPromptTemplate = ({
context,
header,
useFileContext,
fileContextEnabled,
prefixSuffix,
language
}: FimPromptTemplate) => {
const { prefix, suffix } = prefixSuffix
const languageId =
supportedLanguages[language as keyof typeof supportedLanguages]
const fileContext = useFileContext
? `${languageId?.syntaxComments?.start}${context}${languageId?.syntaxComments?.end}`
: ''
const heading = useFileContext && header ? header : ''
const { fileContext, heading } = getFileContext(
fileContextEnabled,
context,
language,
header
)
return `<PRE> ${fileContext}\n${heading}${prefix} <SUF> ${suffix} <MID>`
}

export const getFimPromptTemplateDeepseek = ({
context,
header,
useFileContext,
fileContextEnabled,
prefixSuffix,
language
}: FimPromptTemplate) => {
const { prefix, suffix } = prefixSuffix
const languageId =
supportedLanguages[language as keyof typeof supportedLanguages]
const fileContext = useFileContext
? `${languageId?.syntaxComments?.start}${context}${languageId?.syntaxComments?.end}`
: ''
const heading = useFileContext && header ? header : ''
const { fileContext, heading } = getFileContext(
fileContextEnabled,
context,
language,
header
)
return `<|fim▁begin|>${fileContext}\n${heading}${prefix}<|fim▁hole|>${suffix}<|fim▁end|>`
}

export const getFimPromptTemplateStableCode = ({
context,
header,
useFileContext,
prefixSuffix
fileContextEnabled,
prefixSuffix,
language
}: FimPromptTemplate) => {
const { prefix, suffix } = prefixSuffix
const fileContext = useFileContext ? context : ''
const heading = header ? header : ''
const { fileContext, heading } = getFileContext(
fileContextEnabled,
context,
language,
header
)
return `<fim_prefix>${fileContext}\n${heading}${prefix}<fim_suffix>${suffix}<fim_middle>`
}

Expand Down
8 changes: 4 additions & 4 deletions src/extension/providers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class CompletionProvider implements InlineCompletionItemProvider {
private _statusBar: StatusBarItem
private _temperature = this._config.get('temperature') as number
private _templateProvider: TemplateProvider
private _useFileContext = this._config.get('useFileContext') as boolean
private _fileContextEnabled = this._config.get('fileContextEnabled') as boolean
private _usingFimTemplate = false

constructor(
Expand Down Expand Up @@ -464,7 +464,7 @@ export class CompletionProvider implements InlineCompletionItemProvider {
context: fileInteractionContext || '',
prefixSuffix,
header: this.getPromptHeader(documentLanguage, this._document.uri),
useFileContext: this._useFileContext,
fileContextEnabled: this._fileContextEnabled,
language: documentLanguage
}
)
Expand Down Expand Up @@ -493,7 +493,7 @@ export class CompletionProvider implements InlineCompletionItemProvider {
Original completion: ${this._completion}
Formatted completion: ${formattedCompletion}
Max Lines: ${this._maxLines}
Use file context: ${this._useFileContext}
Use file context: ${this._fileContextEnabled}
Completed lines count ${getLineBreakCount(formattedCompletion)}
Using custom FIM template fim.bhs?: ${this._usingFimTemplate}
`.trim()
Expand Down Expand Up @@ -542,7 +542,7 @@ export class CompletionProvider implements InlineCompletionItemProvider {
this._numLineContext = this._config.get('contextLength') as number
this._numPredictFim = this._config.get('numPredictFim') as number
this._temperature = this._config.get('temperature') as number
this._useFileContext = this._config.get('useFileContext') as boolean
this._fileContextEnabled = this._config.get('fileContextEnabled') as boolean
this._multilineCompletionsEnabled = this._config.get(
'multilineCompletionsEnabled'
) as boolean
Expand Down

0 comments on commit fd3655e

Please sign in to comment.