Skip to content

Commit

Permalink
chore: add otel info for pinecone
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed May 14, 2024
1 parent defd5ab commit f4f5dc7
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 136 deletions.
2 changes: 1 addition & 1 deletion core/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"medici": "^7.0.0",
"mongoose": "8.3.2",
"node-cache": "^5.1.2",
"openai": "^4.43.0",
"openai": "^4.46.1",
"pg": "^8.11.5",
"pino": "9.0.0",
"pino-http": "^10.1.0",
Expand Down
60 changes: 36 additions & 24 deletions core/api/src/services/openai/pinecone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios"

import { env } from "@/config/env"
import { UnknownPineconeError } from "@/domain/support/errors"
import { SemanticAttributes, asyncRunInSpan } from "../tracing"

interface Match {
metadata?: {
Expand All @@ -11,31 +12,42 @@ interface Match {

export const retrieveRelatedQueries = async (
vector: number[],
): Promise<string[] | Error> => {
const topK = 8 // how many results to retrieve
): Promise<string[] | Error> =>
asyncRunInSpan(
"app.chatbot.retrieveRelatedQueries",
{
attributes: {
[SemanticAttributes.CODE_FUNCTION]: "retrieveRelatedQueries",
[SemanticAttributes.CODE_NAMESPACE]: "chatbot",
vector,
},
},
async () => {
const topK = 8 // how many results to retrieve

try {
// TODO: fetch programmatically the endpoint. doc is not clear.
const url = `https://blink-3072-5c8b1i4.svc.aped-4627-b74a.pinecone.io/query`
const headers = {
"Api-Key": env.PINECONE_API_KEY ?? "unknown",
"Content-Type": "application/json",
}
try {
// TODO: fetch programmatically the endpoint. doc is not clear.
const url = `https://blink-3072-5c8b1i4.svc.aped-4627-b74a.pinecone.io/query`
const headers = {
"Api-Key": env.PINECONE_API_KEY ?? "unknown",
"Content-Type": "application/json",
}

const data = {
vector,
topK,
includeMetadata: true,
}
const data = {
vector,
topK,
includeMetadata: true,
}

const response = await axios.post(url, data, { headers })
const response = await axios.post(url, data, { headers })

const results = response.data.matches.map(
(match: Match) => JSON.parse(match.metadata?._node_content ?? "{}").text,
)
return results as string[]
} catch (error) {
console.error(error)
return new UnknownPineconeError(error)
}
}
const results = response.data.matches.map(
(match: Match) => JSON.parse(match.metadata?._node_content ?? "{}").text,
)
return results as string[]
} catch (error) {
console.error(error)
return new UnknownPineconeError(error)
}
},
)
Loading

0 comments on commit f4f5dc7

Please sign in to comment.