1- import * as dotenv from 'dotenv' ;
2- import { OpenAI } from 'openai' ;
1+ import OpenAI from 'openai' ;
32import { Pinecone } from '@pinecone-database/pinecone' ;
43import { HoneyHiveTracer } from 'honeyhive' ;
5-
4+ import dotenv from 'dotenv' ;
65dotenv . config ( ) ;
76
7+ // Initialize HoneyHive Tracer
8+ const tracer = await HoneyHiveTracer . init ( {
9+ apiKey : process . env . HH_API_KEY ,
10+ project : process . env . HH_PROJECT ,
11+ source : "dev" ,
12+ sessionName : "JS RAG Session"
13+ } ) ;
14+
815// Initialize clients
916const openai = new OpenAI ( ) ;
1017const pc = new Pinecone ( ) ;
11- const index = pc . index ( "your-index-name " ) ;
18+ const index = pc . index ( "chunk-size-512 " ) ;
1219
13- async function embedQuery ( query : string ) : Promise < number [ ] > {
20+ async function embedQuery ( query ) {
1421 const res = await openai . embeddings . create ( {
1522 model : "text-embedding-ada-002" ,
1623 input : query
1724 } ) ;
1825 return res . data [ 0 ] . embedding ;
1926}
2027
21- async function getRelevantDocuments ( query : string ) : Promise < string [ ] > {
28+ async function getRelevantDocuments ( query ) {
2229 const queryVector = await embedQuery ( query ) ;
2330 const res = await index . query ( {
2431 vector : queryVector ,
2532 topK : 3 ,
2633 includeMetadata : true
2734 } ) ;
28- return res . matches . map ( item => item . metadata ! . _node_content as string ) ;
35+ return res . matches . map ( item => item . metadata . _node_content ) ;
2936}
3037
31- async function generateResponse ( context : string , query : string ) : Promise < string > {
38+ async function generateResponse ( context , query ) {
3239 const prompt = `Context: ${ context } \n\nQuestion: ${ query } \n\nAnswer:` ;
3340 const response = await openai . chat . completions . create ( {
34- model : "gpt-4 " ,
41+ model : "gpt-4o-mini " ,
3542 messages : [
3643 { role : "system" , content : "You are a helpful assistant." } ,
3744 { role : "user" , content : prompt }
@@ -40,21 +47,13 @@ async function generateResponse(context: string, query: string): Promise<string>
4047 return response . choices [ 0 ] . message . content || "" ;
4148}
4249
43- async function ragPipeline ( query : string ) : Promise < string > {
50+ async function ragPipeline ( query ) {
4451 const docs = await getRelevantDocuments ( query ) ;
4552 const response = await generateResponse ( docs . join ( "\n" ) , query ) ;
4653 return response ;
4754}
4855
4956async function main ( ) {
50- // Initialize HoneyHive Tracer
51- const tracer = await HoneyHiveTracer . init ( {
52- apiKey : process . env . HH_API_KEY ! ,
53- project : process . env . HH_PROJECT ! ,
54- source : "dev" ,
55- sessionName : "TS RAG Session"
56- } ) ;
57-
5857 await tracer . trace ( async ( ) => {
5958 const query = "What does the document talk about?" ;
6059 const response = await ragPipeline ( query ) ;
0 commit comments