“Error: Error loading the fetch-h2 client for the datastax DataAPIClient, error occured at rag application built using langchain, astra db & next js

{“props”:{“pageProps”:{“statusCode”:500}},”page”:”/_error”,”query”:{“__NEXT_PAGE”:”/api/chat”},”buildId”:”development”,”isFallback”:false,”err”:{“name”:”Error”,”source”:”server”,”message”:”Error loading the fetch-h2 client for the DataAPIClient… try setting httpOptions.client to ‘fetch'”,”stack”:”Error: Error loading the fetch-h2 client for the DataAPIClient… try setting httpOptions.client to ‘fetch’n

here is the error occured in rag application.

api route, where is occured when called.

`
“`

import { NextRequest, NextResponse } from ‘next/server’;
import { getVectorStore } from ‘@/lib/astradb’;
import { AIMessage, HumanMessage } from ‘@langchain/core/messages’;
import {
ChatPromptTemplate,
MessagesPlaceholder,
PromptTemplate,
} from ‘@langchain/core/prompts’;
import { ChatOpenAI } from ‘@langchain/openai’;
import { Redis } from ‘@upstash/redis’;
import { Ratelimit } from ‘@upstash/ratelimit’;
import {
LangChainStream,
StreamingTextResponse,
Message as VercelChatMessage,
} from ‘ai’;
import { UpstashRedisCache } from ‘@langchain/community/caches/upstash_redis’;
import { createStuffDocumentsChain } from ‘langchain/chains/combine_documents’;
import { createHistoryAwareRetriever } from ‘langchain/chains/history_aware_retriever’;
import { createRetrievalChain } from ‘langchain/chains/retrieval’;
import https from ‘https’;

const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.fixedWindow(8, ’30s’),
});

export async function POST(req: NextRequest) {

console.log(‘api requested,dddddddddddddddddddddddddddddd’)

try {

const ip = req.ip ?? ‘ip’;
const { success, remaining } = await ratelimit.limit(ip);

// block the request if unsuccessfull
if (!success) {
return new Response(‘Ratelimited!’, { status: 429 });
}

const body = await req.json();
const messages = body.messages;

const chatHistory = messages
.slice(0, -1)
.map((m: VercelChatMessage) =>
m.role === ‘user’
? new HumanMessage(m.content)
: new AIMessage(m.content)
);

const currentMessageContent = messages[messages.length – 1].content;

const cache = new UpstashRedisCache({
client: Redis.fromEnv({
agent: new https.Agent({ keepAlive: true }),
}),
});

const { stream, handlers } = LangChainStream();

const chatModel = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY!,`your text`
modelName: ‘gpt-3.5-turbo’,“your text“
streaming: true,
callbacks: [handlers],
verbose: true,
cache,
});

const rephrasingModel = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY!,
modelName: ‘gpt-3.5-turbo’,
verbose: true,
cache,
});

const retriever = (await getVectorStore()).asRetriever();

const rephrasePrompt = ChatPromptTemplate.fromMessages([
new MessagesPlaceholder(‘chat_history’),
[‘user’, ‘{input}’],
[
‘user’,
‘Given the above conversation, generate a search query to look up in order to get information relevant to the current question. ‘ +
“Don’t leave out any relevant keywords. Only return the query and no other text.”,
],
]);

const historyAwareRetrieverChain = await createHistoryAwareRetriever({
llm: rephrasingModel,
retriever,
rephrasePrompt,
});

const prompt = ChatPromptTemplate.fromMessages([
[
‘system’,
“You are a chatbot for a Decentralized betting website named DecentBet. You impersonate the website’s owner. ” +
“Answer the user’s questions based on the below context. ” +
‘Format your messages in markdown format.nn’ +
‘Context:n{context}’,
],
new MessagesPlaceholder(‘chat_history’),
[‘user’, ‘{input}’],
]);

const combineDocsChain = await createStuffDocumentsChain({
llm: chatModel,
prompt,
// documentPrompt: PromptTemplate.fromTemplate(
// ‘Page URL: {url}nnPage content:n{page_content}’
// ),
documentSeparator: ‘n——–n’,
});

const retrievalChain = await createRetrievalChain({
combineDocsChain,
retriever: historyAwareRetrieverChain,
});

retrievalChain.invoke({
input: currentMessageContent,
chat_history: chatHistory,
});

return new StreamingTextResponse(stream);
} catch (error) {
console.error(“Error:—“,error);
return NextResponse.json({ error: ‘Internal server error’ }, { status: 500 });
}
}

“`

“`

here is route code where error occuring when called, strangely same code working fine with my other application.

astra db instilization:

// import { AstraDB } from ‘@datastax/astra-db-ts’;
import { DataAPIClient } from ‘@datastax/astra-db-ts’;

import { AstraDBVectorStore } from ‘@langchain/community/vectorstores/astradb’;
import { OpenAIEmbeddings } from ‘@langchain/openai’;

const endpoint = process.env.ASTRA_DB_ENDPOINT || ”;
const token = process.env.ASTRA_DB_APPLICATION_TOKEN || ”;
const collection = process.env.ASTRA_DB_COLLECTION || ”;

if (!token || !endpoint || !collection) {
throw new Error(
‘Please set ASTRA_DB_ENDPOINT, ASTRA_DB_APPLICATION_TOKEN, and ASTRA_DB_COLLECTION environment variables.’
);
}

export async function getVectorStore() {
return AstraDBVectorStore.fromExistingIndex(
new OpenAIEmbeddings({ modelName: ‘text-embedding-3-small’ }),
{
token,
endpoint,
collection,

collectionOptions: {
vector: {
dimension: 1536,
metric: ‘cosine’,
},
}
}
);
}
const client = new DataAPIClient(token);
const db = client.db(endpoint);

export async function getEmbeddingsCollection() {
return db.collection(collection);
}

“`

`unable to get over this error , but same code with same dependencies working fine in other app (https://github.com/Sandeepreddyr12/My-Portfolio/blob/main/src/app/api/chat/route.ts).

note : i’ve rewritten latest code from their respective docs, haven’t worked not get over this error.

stack next js, ai lib for streaming, upstash caching, datastax astradb for embeddings.`

please let me know work around to get over this. thank u

New contributor

Sandeepreddy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật