Skip to content

Commit 4c37859

Browse files
committed
Refactor to emulate Vertex response
1 parent ef893c9 commit 4c37859

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

packages/vertexai/src/methods/chrome-adapter.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,23 @@ export class ChromeAdapter {
103103
);
104104
const messages = ChromeAdapter.toLanguageModelMessages(request.contents);
105105
const text = await session.prompt(messages);
106+
return ChromeAdapter.toResponse(text);
107+
}
108+
109+
/**
110+
* Formats string returned by Chrome as a {@link Response} returned by Vertex.
111+
*/
112+
private static toResponse(text: string): Response {
106113
return {
107-
json: () =>
108-
Promise.resolve({
109-
candidates: [
110-
{
111-
content: {
112-
parts: [{ text }]
113-
}
114+
json: async () => ({
115+
candidates: [
116+
{
117+
content: {
118+
parts: [{ text }]
114119
}
115-
]
116-
})
120+
}
121+
]
122+
})
117123
} as Response;
118124
}
119125

@@ -131,7 +137,10 @@ export class ChromeAdapter {
131137
const stream = await session.promptStreaming(messages);
132138
return ChromeAdapter.toStreamResponse(stream);
133139
}
134-
// Formats string stream returned by Chrome as SSE returned by Vertex.
140+
141+
/**
142+
* Formats string stream returned by Chrome as SSE returned by Vertex.
143+
*/
135144
private static async toStreamResponse(
136145
stream: ReadableStream<string>
137146
): Promise<Response> {

0 commit comments

Comments
 (0)