Skip to content

Commit

Permalink
Handle AIMessageChunk, attempt to gather non-dict values
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed Nov 27, 2023
1 parent 477af9e commit 0a892dc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/conversational_retrieval_chain/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ChatHistory(BaseModel):

chat_history: List[Tuple[str, str]] = Field(
...,
extra={"widget": {"type": "chat", "input": "question", "output": "output"}},
extra={"widget": {"type": "chat", "input": "question"}},
)
question: str

Expand Down
2 changes: 1 addition & 1 deletion examples/widgets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ChatHistory(BaseModel):
class ChatHistoryMessage(BaseModel):
chat_history: List[BaseMessage] = Field(
...,
extra={"widget": {"type": "chat", "input": "location", "output": "output"}},
extra={"widget": {"type": "chat", "input": "location"}},
)
location: str

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
import { AutosizeTextarea } from "./AutosizeTextarea";
import { isJsonSchemaExtra } from "../utils/schema";
import { useStreamCallback } from "../useStreamCallback";
import { traverseNaiveJsonPath } from "../utils/path";
import { getNormalizedJsonPath, traverseNaiveJsonPath } from "../utils/path";
import { getMessageContent } from "../utils/messages";

type MessageTuple = [string, string];

Expand Down Expand Up @@ -53,9 +54,22 @@ export const ChatMessageTuplesControlRenderer = withJsonFormsControlProps(
const widget = props.schema.extra.widget;
if (!("input" in widget) && !("output" in widget)) return;

const human = traverseNaiveJsonPath(ctx.input, widget.input ?? "");
const ai = traverseNaiveJsonPath(ctx.output, widget.output ?? "");
const inputPath = getNormalizedJsonPath(widget.input ?? "");
const outputPath = getNormalizedJsonPath(widget.output ?? "");

const isSingleOutputKey =
ctx.output != null &&
Object.keys(ctx.output).length === 1 &&
Object.keys(ctx.output)[0] === "output";

const human = traverseNaiveJsonPath(ctx.input, inputPath);
let ai = traverseNaiveJsonPath(ctx.output, outputPath);

if (isSingleOutputKey) {
ai = traverseNaiveJsonPath(ai, ["output", ...outputPath]) ?? ai;
}

ai = getMessageContent(ai);
if (typeof human === "string" && typeof ai === "string") {
props.handleChange(props.path, [...data, [human, ai]]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@jsonforms/core";
import { AutosizeTextarea } from "./AutosizeTextarea";
import { useStreamCallback } from "../useStreamCallback";
import { traverseNaiveJsonPath } from "../utils/path";
import { getNormalizedJsonPath, traverseNaiveJsonPath } from "../utils/path";
import { isJsonSchemaExtra } from "../utils/schema";
import * as ToggleGroup from "@radix-ui/react-toggle-group";

Expand Down Expand Up @@ -122,8 +122,20 @@ export const ChatMessagesControlRenderer = withJsonFormsControlProps(
const widget = props.schema.extra.widget;
if (!("input" in widget) && !("output" in widget)) return;

const human = traverseNaiveJsonPath(ctx.input, widget.input ?? "");
const ai = traverseNaiveJsonPath(ctx.output, widget.output ?? "");
const inputPath = getNormalizedJsonPath(widget.input ?? "");
const outputPath = getNormalizedJsonPath(widget.output ?? "");

const human = traverseNaiveJsonPath(ctx.input, inputPath);
let ai = traverseNaiveJsonPath(ctx.output, outputPath);

const isSingleOutputKey =
ctx.output != null &&
Object.keys(ctx.output).length === 1 &&
Object.keys(ctx.output)[0] === "output";

if (isSingleOutputKey) {
ai = traverseNaiveJsonPath(ai, ["output", ...outputPath]) ?? ai;
}

const humanMsg = constructMessage(human, "human");
const aiMsg = constructMessage(ai, "ai");
Expand Down
7 changes: 7 additions & 0 deletions langserve/playground/src/utils/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getMessageContent(x: unknown) {
if (typeof x === "string") return x;
if (typeof x === "object" && x != null) {
if ("content" in x && typeof x.content === "string") return x.content;
}
return null;
}
9 changes: 8 additions & 1 deletion langserve/playground/src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ function isAccessibleObject(x: unknown): x is Record<string | number, unknown> {
return typeof x === "object" && x != null;
}

export function getNormalizedJsonPath(
path: string | number | Array<string | number>
) {
return Array.isArray(path) ? path : [path];
}

export function traverseNaiveJsonPath(
x: unknown,
path: string | number | Array<string | number>
) {
const queue = Array.isArray(path) ? path : [path];
const queue = getNormalizedJsonPath(path);

let tmp: unknown = x;
while (queue.length > 0) {
const first = queue.shift()!;
Expand Down

0 comments on commit 0a892dc

Please sign in to comment.