Skip to content

Commit

Permalink
docs(semantic): add Hyde Code and Keywords strategy documentation
Browse files Browse the repository at this point in the history
Added documentation for Hyde Code and Keywords strategy with detailed steps and code snippets.
  • Loading branch information
phodal committed May 20, 2024
1 parent 321deb6 commit dc705ff
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 31 deletions.
3 changes: 2 additions & 1 deletion docs/development/prompt-example.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
layout: default
title: Prompt Example
title: Unit Test Prompt Example
nav_order: 999
parent: Development
---

```devin
Expand Down
43 changes: 43 additions & 0 deletions docs/semantic/hyde-code-strategy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: default
title: Hyde Code Strategy
nav_order: 3
parent: Semantic
---

Code file: HydeCodeStrategy.ts

1. generate hyde doc code from the user query
2. retrieve code snippets by hyde code from the codebase
3. summarize the code snippets and return the result

```typescript
channel.appendLine("=".repeat(80));
channel.appendLine(`= Hyde Keywords Strategy: ${this.constructor.name} =`);
channel.appendLine("=".repeat(80));

this.step = HydeStep.Propose;
let documents = await this.generateDocument();
let hydeCode = documents.content;

this.step = HydeStep.Retrieve;
let chunks = await this.retrieveChunks(hydeCode);

this.step = HydeStep.Evaluate;
let evaluateContext: KeywordEvaluateContext = {
step: this.step,
question: this.query,
code: chunks.map(item => item.text).join("\n"),
language: ""
};

if (chunks.length === 0) {
channel.appendLine("No code snippets found.");
return new StrategyOutput("", []);
}

channel.appendLine("\n");
channel.appendLine(" --- Summary --- ");
let evaluateIns = await PromptManager.getInstance().renderHydeTemplate(this.step, this.documentType, evaluateContext);
return new StrategyOutput(await executeIns(evaluateIns), chunks);
```
40 changes: 40 additions & 0 deletions docs/semantic/hyde-keywords-strategy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: default
title: Hyde Keywords Strategy
nav_order: 2
parent: Semantic
---

Code file: HydeKeywordsStrategy.ts

1. generate keywords from the user query
2. retrieve code snippets by query from the codebase
3. summarize the code snippets and return the result

```typescript
this.step = HydeStep.Propose;
let documents = await this.generateDocument();
let keywords = documents.content;

this.step = HydeStep.Retrieve;
let queryTerm = this.createQueryTerm(keywords);
let chunkItems = await this.retrieveChunks(queryTerm);

this.step = HydeStep.Evaluate;
let evaluateContext: KeywordEvaluateContext = {
step: this.step,
question: keywords.question,
code: chunkItems.map(item => item.text).join("\n"),
language: ""
};

if (chunkItems.length === 0) {
channel.appendLine("No code snippets found.");
return new StrategyOutput("", []);
}

channel.appendLine("\n");
channel.appendLine(" --- Summary --- ");
let evaluateIns = await PromptManager.getInstance().renderHydeTemplate(this.step, this.documentType, evaluateContext);
return new StrategyOutput(await executeIns(evaluateIns), chunkItems);
```
4 changes: 2 additions & 2 deletions docs/semantic/rag-strategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ parent: Semantic

### Our Strategy

- HydeCodeStrategy
- HydeKeywordsStrategy
- HydeCodeStrategy reference to GitHub Copilot
- HydeKeywordsStrategy reference to Bloop: HydeDoc

5 changes: 2 additions & 3 deletions src/code-search/search-strategy/HydeCodeStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChunkItem } from "../embedding/_base/Embedding";
import { HydeQuery, HydeStrategy } from "./_base/HydeStrategy";
import { executeIns, HydeQuery, HydeStrategy } from "./_base/HydeStrategy";
import { HydeDocument, HydeDocumentType } from "./_base/HydeDocument";
import { AutoDevExtension } from "../../AutoDevExtension";
import { ChatMessage } from "../../llm-provider/ChatMessage";
Expand All @@ -9,8 +9,7 @@ import { channel } from "../../channel";
import { LocalEmbeddingProvider } from "../embedding/LocalEmbeddingProvider";
import { DefaultRetrieval } from "../retrieval/DefaultRetrieval";
import { ContextItem, RetrieveOption } from "../retrieval/Retrieval";
import { TextRange } from "../scope-graph/model/TextRange";
import { executeIns, KeywordEvaluateContext, KeywordsProposeContext } from "./HydeKeywordsStrategy";
import { KeywordEvaluateContext, KeywordsProposeContext } from "./HydeKeywordsStrategy";
import { StreamingMarkdownCodeBlock } from "../../markdown/StreamingMarkdownCodeBlock";
import { StrategyOutput } from "./_base/StrategyOutput";

Expand Down
26 changes: 1 addition & 25 deletions src/code-search/search-strategy/HydeKeywordsStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HydeQuery, HydeStrategy } from "./_base/HydeStrategy";
import { executeIns, HydeQuery, HydeStrategy } from "./_base/HydeStrategy";
import { ChunkItem } from "../embedding/_base/Embedding";
import { HydeDocument, HydeDocumentType } from "./_base/HydeDocument";
import { PromptManager } from "../../prompt-manage/PromptManager";
Expand All @@ -8,35 +8,11 @@ import { ChatMessage } from "../../llm-provider/ChatMessage";
import { HydeKeywords } from "./_base/HydeKeywords";
import { DefaultRetrieval } from "../retrieval/DefaultRetrieval";
import { AutoDevExtension } from "../../AutoDevExtension";
import { CustomActionPrompt } from "../../prompt-manage/custom-action/CustomActionPrompt";
import { AutoDevStatus, AutoDevStatusManager } from "../../editor/editor-api/AutoDevStatusManager";
import { LlmProvider } from "../../llm-provider/LlmProvider";
import { channel } from "../../channel";
import { LocalEmbeddingProvider } from "../embedding/LocalEmbeddingProvider";
import { ContextItem, RetrieveOption } from "../retrieval/Retrieval";
import { StrategyOutput } from "./_base/StrategyOutput";

export async function executeIns(instruction: string) {
console.log("\ninstruction: \n" + instruction);
let result = "";
try {
let chatMessages = CustomActionPrompt.parseChatMessage(instruction);
AutoDevStatusManager.instance.setStatus(AutoDevStatus.InProgress);
let response = await LlmProvider.codeCompletion()._streamChat(chatMessages);
for await (let chatMessage of response) {
channel.append(chatMessage.content);
result += chatMessage.content;
}

AutoDevStatusManager.instance.setStatus(AutoDevStatus.Done);
return result;
} catch (e) {
console.log("error:" + e);
AutoDevStatusManager.instance.setStatus(AutoDevStatus.Error);
return "";
}
}

/**
* The `HydeKeywordsStrategy` class is a part of the Hyde Strategy pattern and is used to generate keywords from a query.
* These keywords are then used to retrieve similar code by symbols.
Expand Down
24 changes: 24 additions & 0 deletions src/code-search/search-strategy/_base/HydeStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ChunkItem, Embedding } from "../../embedding/_base/Embedding";
import { HydeDocument, HydeDocumentType } from "./HydeDocument";
import { StrategyOutput } from "./StrategyOutput";
import { CustomActionPrompt } from "../../../prompt-manage/custom-action/CustomActionPrompt";
import { AutoDevStatus, AutoDevStatusManager } from "../../../editor/editor-api/AutoDevStatusManager";
import { LlmProvider } from "../../../llm-provider/LlmProvider";
import { channel } from "../../../channel";

export type HydeQuery = string | RegExp | Embedding;

Expand Down Expand Up @@ -64,3 +68,23 @@ export interface HydeStrategy<T> {
execute(): Promise<StrategyOutput>;
}

export async function executeIns(instruction: string) {
console.log("\ninstruction: \n" + instruction);
let result = "";
try {
let chatMessages = CustomActionPrompt.parseChatMessage(instruction);
AutoDevStatusManager.instance.setStatus(AutoDevStatus.InProgress);
let response = await LlmProvider.codeCompletion()._streamChat(chatMessages);
for await (let chatMessage of response) {
channel.append(chatMessage.content);
result += chatMessage.content;
}

AutoDevStatusManager.instance.setStatus(AutoDevStatus.Done);
return result;
} catch (e) {
console.log("error:" + e);
AutoDevStatusManager.instance.setStatus(AutoDevStatus.Error);
return "";
}
}

0 comments on commit dc705ff

Please sign in to comment.