forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: unify response and agent response (run-llama#930)
- Loading branch information
1 parent
834f492
commit 436bc41
Showing
29 changed files
with
217 additions
and
195 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"llamaindex": minor | ||
--- | ||
|
||
Unify chat engine response and agent response |
This file contains 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 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 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 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 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 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 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 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 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 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 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,90 @@ | ||
import type { NodeWithScore } from "./Node.js"; | ||
import type { | ||
ChatMessage, | ||
ChatResponse, | ||
ChatResponseChunk, | ||
} from "./llm/types.js"; | ||
import { extractText } from "./llm/utils.js"; | ||
|
||
export class EngineResponse implements ChatResponse, ChatResponseChunk { | ||
sourceNodes?: NodeWithScore[]; | ||
|
||
metadata: Record<string, unknown> = {}; | ||
|
||
message: ChatMessage; | ||
raw: object | null; | ||
|
||
#stream: boolean; | ||
|
||
private constructor( | ||
chatResponse: ChatResponse, | ||
stream: boolean, | ||
sourceNodes?: NodeWithScore[], | ||
) { | ||
this.message = chatResponse.message; | ||
this.raw = chatResponse.raw; | ||
this.sourceNodes = sourceNodes; | ||
this.#stream = stream; | ||
} | ||
|
||
static fromResponse( | ||
response: string, | ||
stream: boolean, | ||
sourceNodes?: NodeWithScore[], | ||
): EngineResponse { | ||
return new EngineResponse( | ||
EngineResponse.toChatResponse(response), | ||
stream, | ||
sourceNodes, | ||
); | ||
} | ||
|
||
private static toChatResponse( | ||
response: string, | ||
raw: object | null = null, | ||
): ChatResponse { | ||
return { | ||
message: { | ||
content: response, | ||
role: "assistant", | ||
}, | ||
raw, | ||
}; | ||
} | ||
|
||
static fromChatResponse( | ||
chatResponse: ChatResponse, | ||
sourceNodes?: NodeWithScore[], | ||
): EngineResponse { | ||
return new EngineResponse(chatResponse, false, sourceNodes); | ||
} | ||
|
||
static fromChatResponseChunk( | ||
chunk: ChatResponseChunk, | ||
sourceNodes?: NodeWithScore[], | ||
): EngineResponse { | ||
return new EngineResponse( | ||
this.toChatResponse(chunk.delta, chunk.raw), | ||
true, | ||
sourceNodes, | ||
); | ||
} | ||
|
||
// @deprecated use 'message' instead | ||
get response(): string { | ||
return extractText(this.message.content); | ||
} | ||
|
||
get delta(): string { | ||
if (!this.#stream) { | ||
console.warn( | ||
"delta is only available for streaming responses. Consider using 'message' instead.", | ||
); | ||
} | ||
return extractText(this.message.content); | ||
} | ||
|
||
toString() { | ||
return this.response ?? ""; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.