use custom fetch and streaming in watsonx

This commit is contained in:
Adam Shedivy 2024-08-20 10:55:21 -05:00
parent 869752a4d5
commit ac09211a82
2 changed files with 8 additions and 15 deletions

View file

@ -6,6 +6,7 @@ import {
} from "../../index.js";
import { stripImages } from "../images.js";
import { BaseLLM } from "../index.js";
import { streamResponse } from "../stream.js";
const watsonxConfig = {
accessToken: {
expiration: 0,
@ -186,7 +187,7 @@ class WatsonX extends BaseLLM {
const stopToken =
this.watsonxStopToken ??
(options.model?.includes("granite") ? "<|im_end|>" : undefined);
var streamResponse = await fetch(
var response = await this.fetch(
`${this.watsonxUrl}/ml/v1/text/generation_stream?version=2023-05-29`,
{
method: "POST",
@ -212,25 +213,17 @@ class WatsonX extends BaseLLM {
},
);
if (!streamResponse.ok || streamResponse.body === null) {
if (!response.ok || response.body === null) {
throw new Error(
"Something went wrong. No response received, check your connection",
);
} else {
const reader = streamResponse.body.getReader();
while (true) {
const chunk = await reader.read();
if (chunk.done) {
break;
}
// Decode the stream
const textResponseMsg = new TextDecoder().decode(chunk.value);
const lines = textResponseMsg.split(/\r?\n/);
for await (const value of streamResponse(response)) {
const lines = value.split("\n");
let generatedChunk = "";
let generatedTextIndex = undefined;
lines.forEach((el) => {
lines.forEach((el: string) => {
// console.log(`${el}`);
if (el.startsWith("id:")) {
generatedTextIndex = parseInt(el.replace(/^id:\s+/, ""));

View file

@ -18,11 +18,11 @@ To authenticate to watsonx.ai SaaS with Continue, you will need to create a proj
### watsonx.ai Software
To authenticate to your watsonx.ai Software instance with Continue, you can use either `username/password` or `ZenApiKey` method:
1. *Option 1*: using username/password authentication:
1. *Option 1*: using `username/password` authentication:
- Set **watsonx URL** to your watsonx software endpoint, e.g. `https://cpd-watsonx.apps.example.com`.
- Set **watsonx Project ID** to your watsonx project ID.
- Set **watsonx Credentials** to your watsonx Username and Password using `username:password` as format.
2. *Option 2*: using API Key authentication:
2. *Option 2*: using `ZenApiKey` authentication:
- Set **watsonx URL** to your watsonx software endpoint, e.g. `https://cpd-watsonx.apps.example.com`.
- Set **watsonx Project ID** to your watsonx project ID.
- Set **watsonx Credentials** to your watsonx Zen API Key. To generate it: