SDK examples
SDK examples
RAG retrieval
Learn how to use RAG retrieval with the Latitude SDK
Prompt
In your prompt, you can define a tool that will be used to retrieve information.
---
provider: Latitude
model: gpt-4o-mini
temperature: 0.7
tools:
- get_answer:
description: Ask this tool for the answer when user do a question.
parameters:
type: object
additionalProperties: false
required: ['question']
properties:
question:
type: string
description: Question to ask
---
Give user's question {{ question }} a concise answer.
Code
Performing RAG retrieval with the Latitude SDK simply involves defining a tool in your prompt. In your code, you can then get the results from the RAG solution you use.
import { Pinecone } from '@pinecone-database/pinecone'
import OpenAI from 'openai'
import { Latitude } from '@latitude-data/sdk'
type Tools = { get_answer: { question: string } }
const PINECODE_INDEX_NAME = 'geography-quizz-index'
async function run() {
const sdk = new Latitude(process.env.LATITUDE_API_KEY, {
projectId: Number(process.env.PROJECT_ID),
versionUuid: 'live',
})
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
const pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY })
const pc = pinecone.Index(PINECODE_INDEX_NAME)
const question = 'What is the deepest ocean in the world?'
console.log('Question: ', question)
console.log('\nSearching...\n')
const result = await sdk.prompts.run<Tools>('rag-retrieval/example', {
parameters: { question },
tools: {
get_answer: async ({ question }) => {
// Do the embedding
const embedding = await openai.embeddings
.create({
input: question,
model: 'text-embedding-3-small',
})
.then((res) => res.data[0].embedding)
// Query your RAG backend. In this case, Pinecone
const queryResponse = await pc.query({
vector: embedding,
topK: 10,
includeMetadata: true,
})
const first = queryResponse.matches[0]
return first?.metadata?.answer
},
},
})
console.log('Answer: ', result.response.text)
}
run()
Assistant
Responses are generated using AI and may contain mistakes.