Skip to content

Commit

Permalink
LLM-enhanced keyword context (#52815)
Browse files Browse the repository at this point in the history
* Adds `fast` parameter to completions endpoint and `fastChatModel`
param to site config. This is intended for faster chat models that are
useful for simple generations.
* Use the fast chat model to generate a local keyword search. This
replaces the old keyword search mechanism, which stemmed/lemmatized
every word in the user query.
* Use the fast chat model to generate a small set of file fragments to
search for. This is mainly useful for surfacing READMEs for questions
like "What does this project do?"
* Update the set of "files read" presented in the UI to include only
those files actually read into the context window. Previously, we were
showing all files returned by the context fetcher, but in reality, only
a subset of these would fit into the context window.
  • Loading branch information
beyang authored and ErikaRS committed Jun 22, 2023
1 parent 60f9453 commit 30141f5
Show file tree
Hide file tree
Showing 60 changed files with 850 additions and 446 deletions.
3 changes: 2 additions & 1 deletion client/cody-cli/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ async function startCLI() {
}
}

const finalPrompt = await transcript.toPrompt(getPreamble(codebase))
const { prompt: finalPrompt, contextFiles } = await transcript.getPromptForLastInteraction(getPreamble(codebase))
transcript.setUsedContextFilesForLastInteraction(contextFiles)

let text = ''
streamCompletions(completionsClient, finalPrompt, {
Expand Down
5 changes: 3 additions & 2 deletions client/cody-cli/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CodebaseContext } from '@sourcegraph/cody-shared/src/codebase-context'
import { SourcegraphEmbeddingsSearchClient } from '@sourcegraph/cody-shared/src/embeddings/client'
import { KeywordContextFetcher } from '@sourcegraph/cody-shared/src/keyword-context'
import { KeywordContextFetcher } from '@sourcegraph/cody-shared/src/local-context'
import { SourcegraphGraphQLAPIClient } from '@sourcegraph/cody-shared/src/sourcegraph-api/graphql'
import { isError } from '@sourcegraph/cody-shared/src/utils'

Expand All @@ -26,7 +26,8 @@ export async function createCodebaseContext(
{ useContext: contextType, serverEndpoint },
codebase,
embeddingsSearch,
new LocalKeywordContextFetcherMock()
new LocalKeywordContextFetcherMock(),
null
)

return codebaseContext
Expand Down
3 changes: 2 additions & 1 deletion client/cody-cli/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export async function interactionFromMessage(
new Interaction(
{ speaker: 'human', text, displayText: text },
{ speaker: 'assistant', text: '', displayText: '' },
contextMessages
contextMessages,
[]
)
)
}
5 changes: 4 additions & 1 deletion client/cody-shared/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion client/cody-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
},
"dependencies": {
"@sourcegraph/common": "workspace:*",
"@sourcegraph/http-client": "workspace:*"
"@sourcegraph/http-client": "workspace:*",
"xml2js": "^0.6.0"
},
"devDependencies": {
"@types/xml2js": "^0.4.11"
}
}
15 changes: 12 additions & 3 deletions client/cody-shared/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Message } from '../sourcegraph-api'
import type { SourcegraphCompletionsClient } from '../sourcegraph-api/completions/client'
import type { CompletionParameters, CompletionCallbacks } from '../sourcegraph-api/completions/types'

const DEFAULT_CHAT_COMPLETION_PARAMETERS: Omit<CompletionParameters, 'messages'> = {
type ChatParameters = Omit<CompletionParameters, 'messages'>

const DEFAULT_CHAT_COMPLETION_PARAMETERS: ChatParameters = {
temperature: 0.2,
maxTokensToSample: SOLUTION_TOKEN_LENGTH,
topK: -1,
Expand All @@ -13,10 +15,17 @@ const DEFAULT_CHAT_COMPLETION_PARAMETERS: Omit<CompletionParameters, 'messages'>
export class ChatClient {
constructor(private completions: SourcegraphCompletionsClient) {}

public chat(messages: Message[], cb: CompletionCallbacks): () => void {
public chat(messages: Message[], cb: CompletionCallbacks, params?: Partial<ChatParameters>): () => void {
const isLastMessageFromHuman = messages.length > 0 && messages[messages.length - 1].speaker === 'human'
const augmentedMessages = isLastMessageFromHuman ? messages.concat([{ speaker: 'assistant' }]) : messages

return this.completions.stream({ messages: augmentedMessages, ...DEFAULT_CHAT_COMPLETION_PARAMETERS }, cb)
return this.completions.stream(
{
...DEFAULT_CHAT_COMPLETION_PARAMETERS,
...params,
messages: augmentedMessages,
},
cb
)
}
}
7 changes: 4 additions & 3 deletions client/cody-shared/src/chat/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function createClient({

const embeddingsSearch = repoId ? new SourcegraphEmbeddingsSearchClient(graphqlClient, repoId, true) : null

const codebaseContext = new CodebaseContext(config, config.codebase, embeddingsSearch, null)
const codebaseContext = new CodebaseContext(config, config.codebase, embeddingsSearch, null, null)

const intentDetector = new SourcegraphIntentDetectorClient(graphqlClient)

Expand Down Expand Up @@ -116,10 +116,11 @@ export async function createClient({

sendTranscript()

const prompt = await transcript.toPrompt(getPreamble(config.codebase))
const { prompt, contextFiles } = await transcript.getPromptForLastInteraction(getPreamble(config.codebase))
transcript.setUsedContextFilesForLastInteraction(contextFiles)

const responsePrefix = interaction.getAssistantMessage().prefix ?? ''
let rawText = ''

chatClient.chat(prompt, {
onChange(_rawText) {
rawText = _rawText
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/chat-question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class ChatQuestion implements Recipe {
context.intentDetector,
context.codebaseContext,
context.editor.getActiveTextEditorSelection() || null
)
),
[]
)
)
}
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/context-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class ContextSearch implements Recipe {
text: '',
displayText: await this.displaySearchResults(truncatedText, context.codebaseContext, wsRootPath),
},
new Promise(resolve => resolve([]))
new Promise(resolve => resolve([])),
[]
)
}

Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/explain-code-detailed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class ExplainCodeDetailed implements Recipe {
truncatedFollowingText,
selection,
context.codebaseContext
)
),
[]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class ExplainCodeHighLevel implements Recipe {
truncatedFollowingText,
selection,
context.codebaseContext
)
),
[]
)
}
}
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/file-touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export class FileTouch implements Recipe {
speaker: 'assistant',
prefix: 'Working on it! I will notify you when the file is ready.\n',
},
this.getContextMessages(selection, currentDir)
this.getContextMessages(selection, currentDir),
[]
)
)
}
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/find-code-smells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ If you have no ideas because the code looks fine, feel free to say that it alrea
prefix: assistantResponsePrefix,
text: assistantResponsePrefix,
},
new Promise(resolve => resolve([]))
new Promise(resolve => resolve([])),
[]
)
}
}
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/fixup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export class Fixup implements Recipe {
speaker: 'assistant',
prefix: 'Check your document for updates from Cody.\n',
},
this.getContextMessages(selection.selectedText, context.codebaseContext)
this.getContextMessages(selection.selectedText, context.codebaseContext),
[]
)
)
}
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/generate-docstring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class GenerateDocstring implements Recipe {
truncatedFollowingText,
selection,
context.codebaseContext
)
),
[]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class PrDescription implements Recipe {
prefix: emptyGitCommitMessage,
text: emptyGitCommitMessage,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}

Expand All @@ -71,7 +72,8 @@ export class PrDescription implements Recipe {
prefix: assistantResponsePrefix,
text: assistantResponsePrefix,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}
}
6 changes: 4 additions & 2 deletions client/cody-shared/src/chat/recipes/generate-release-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class ReleaseNotes implements Recipe {
prefix: emptyGitLogMessage,
text: emptyGitLogMessage,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}

Expand All @@ -91,7 +92,8 @@ export class ReleaseNotes implements Recipe {
prefix: assistantResponsePrefix,
text: assistantResponsePrefix,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}
}
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/generate-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class GenerateTest implements Recipe {
truncatedFollowingText,
selection,
context.codebaseContext
)
),
[]
)
}
}
6 changes: 4 additions & 2 deletions client/cody-shared/src/chat/recipes/git-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export class GitHistory implements Recipe {
prefix: emptyGitLogMessage,
text: emptyGitLogMessage,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}

Expand All @@ -84,7 +85,8 @@ export class GitHistory implements Recipe {
prefix: assistantResponsePrefix,
text: assistantResponsePrefix,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class ImproveVariableNames implements Recipe {
truncatedFollowingText,
selection,
context.codebaseContext
)
),
[]
)
}
}
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/inline-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class InlineAssist implements Recipe {
displayText,
},
{ speaker: 'assistant' },
this.getContextMessages(truncatedText, context.codebaseContext, selection, context.editor)
this.getContextMessages(truncatedText, context.codebaseContext, selection, context.editor),
[]
)
)
}
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/next-questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class NextQuestions implements Recipe {
prefix: assistantResponsePrefix,
text: assistantResponsePrefix,
},
this.getContextMessages(promptMessage, context.editor, context.intentDetector, context.codebaseContext)
this.getContextMessages(promptMessage, context.editor, context.intentDetector, context.codebaseContext),
[]
)
)
}
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/non-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export class NonStop implements Recipe {
speaker: 'assistant',
prefix: 'Check your document for updates from Cody.',
},
this.getContextMessages(selection.selectedText, context.codebaseContext)
this.getContextMessages(selection.selectedText, context.codebaseContext),
[]
)
)
}
Expand Down
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/optimize-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ However if no optimization is possible; just say the code is already optimized.
truncatedFollowingText,
selection,
context.codebaseContext
)
),
[]
)
}
}
3 changes: 2 additions & 1 deletion client/cody-shared/src/chat/recipes/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class TranslateToLanguage implements Recipe {
prefix: assistantResponsePrefix,
text: assistantResponsePrefix,
},
Promise.resolve([])
Promise.resolve([]),
[]
)
}
}

0 comments on commit 30141f5

Please sign in to comment.