Skip to content

Commit

Permalink
Merge pull request #6 from pinecone-io/update-to-use-azure-serverless
Browse files Browse the repository at this point in the history
Updated to use Azure serverless as the default Pinecone option.
  • Loading branch information
cwaddingham authored Jul 17, 2024
2 parents 4b68a4e + 749f997 commit c08ebe7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
6 changes: 5 additions & 1 deletion .env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ SERVICE_WEB_CUSTOM_DOMAIN_CERT_ID=
# Pinecone settings
PINECONE_API_KEY=
OPENAI_API_KEY=
PINECONE_REGION="us-west-2"
PINECONE_CLOUD="azure"
PINECONE_REGION="eastus2"
PINECONE_INDEX="pinecone-azd-rag-demo"

# Azure settings
AZURE_LOCATION="eastus2"
6 changes: 3 additions & 3 deletions src/app/api/checkIndex/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export async function POST() {
// Select the desired index
try {
await getOrCreateIndex(pinecone, INDEX_NAME)
} catch (e) {
console.error(e)
} catch (error) {
console.error(error)
return NextResponse.json({
success: false,
error: e.message || 'An unexpected error occurred'
error: (error as Error).cause || 'An unexpected error occurred'
})
}
const index = await pinecone.Index(INDEX_NAME)
Expand Down
7 changes: 3 additions & 4 deletions src/app/hooks/useInitializeIndex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect, useState, useCallback } from 'react'
import { Pinecone } from '@pinecone-database/pinecone'
import { connectPinecone, getOrCreateIndex, INDEX_NAME } from '@/lib/pinecone'

const useInitializeIndex = () => {
Expand All @@ -9,12 +8,12 @@ const useInitializeIndex = () => {
const initializeIndex = useCallback(async () => {
try {
const pinecone = connectPinecone()
console.log('Connected to Pinecone')
console.log('Connected to Pinecone, initializing index ', INDEX_NAME)
await getOrCreateIndex(pinecone, INDEX_NAME)
console.log('Connected to index', INDEX_NAME)
console.log('Connected to index ', INDEX_NAME)
setIsInitialized(true)
} catch (e: any) {
console.error(e)
console.error('Error initializing index', e)
setError(e.message || 'An unexpected error occurred')
}
}, [])
Expand Down
26 changes: 14 additions & 12 deletions src/app/lib/pinecone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreateIndexOptions, Pinecone, Index } from '@pinecone-database/pinecone'
import { Pinecone, Index } from '@pinecone-database/pinecone'
import {
CreateIndexRequestMetricEnum,
CreateIndexRequestSpec,
Expand All @@ -18,8 +18,6 @@ export type IndexProperties = {
dimension: number
waitUntilReady: boolean
metric: CreateIndexRequestMetricEnum
region: string
cloud: 'aws' | 'gcp' | 'azure'
spec: CreateIndexRequestSpec
}

Expand All @@ -42,30 +40,34 @@ export async function getOrCreateIndex(
indexName: string = process.env.PINECONE_INDEX || 'pinecone-azd-rag-demo',
dimension: number = 1536,
metric: CreateIndexRequestMetricEnum = 'cosine',
region: string = PINECONE_REGION,
cloud: 'aws' | 'gcp' | 'azure' = PINECONE_CLOUD as 'aws' | 'gcp' | 'azure'
spec: CreateIndexRequestSpec = {
serverless: {
region: PINECONE_REGION,
cloud: PINECONE_CLOUD
}
}
) {
console.log('Checking for index ', indexName)
const indexList = await pc.listIndexes()
const indexes = indexList.indexes
console.log('Indexes: ', indexes)
const indexExists =
indexes && indexes.some((index) => index.name === indexName)
if (!indexExists) {
// Create Pinecone index if it does not exist
console.log('Creating index ', indexName)
var properties: IndexProperties = {
name: indexName,
dimension: dimension,
waitUntilReady: true,
metric: metric,
spec: {
serverless: {
region: region,
cloud: cloud as ServerlessSpecCloudEnum
}
}
spec: spec
}
try {
await pc.createIndex(properties);
await pc.createIndex(properties)
} catch (error) {
console.error('Error creating index: ', error)
console.log('Properties: ', properties)
throw error
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/app/services/chunkedUpsert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Index, PineconeRecord } from '@pinecone-database/pinecone';
import type { Index, PineconeRecord } from '@pinecone-database/pinecone'

const sliceIntoChunks = <T>(arr: T[], chunkSize: number) => {
return Array.from({ length: Math.ceil(arr.length / chunkSize) }, (_, i) =>
Expand All @@ -13,22 +13,22 @@ export const chunkedUpsert = async (
chunkSize = 10
) => {
// Split the vectors into chunks
const chunks = sliceIntoChunks<PineconeRecord>(vectors, chunkSize);
const chunks = sliceIntoChunks<PineconeRecord>(vectors, chunkSize)

try {
// Upsert each chunk of vectors into the index
await Promise.allSettled(
chunks.map(async (chunk) => {
try {
await index.namespace(namespace).upsert(vectors);
await index.namespace(namespace).upsert(vectors)
} catch (e) {
console.log('Error upserting chunk', e);
console.log('Error upserting chunk', e)
}
})
);

return true;
return true
} catch (e) {
throw new Error(`Error upserting vectors into index: ${e}`);
throw new Error(`Error upserting vectors into index: ${e}`)
}
};
}
28 changes: 11 additions & 17 deletions src/app/services/pinecone.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import { Pinecone, type ScoredPineconeRecord } from "@pinecone-database/pinecone";
import { connectPinecone, getOrCreateIndex, INDEX_NAME } from "@/lib/pinecone";

export type Metadata = {
url: string,
text: string,
chunk: string,
hash: string
}
import { connectPinecone, getOrCreateIndex, INDEX_NAME } from '@/lib/pinecone'

// The function `getMatchesFromEmbeddings` is used to retrieve matches for the given embeddings
const getMatchesFromEmbeddings = async (embeddings: number[], topK: number, namespace: string): Promise<ScoredPineconeRecord<Metadata>[]> => {
const getMatchesFromEmbeddings = async (
embeddings: number[],
topK: number,
namespace: string
) => {
// Obtain a client for Pinecone
const pinecone = await connectPinecone();
const pinecone = await connectPinecone()

// Get the Pinecone index
const index = await getOrCreateIndex(pinecone, INDEX_NAME);
const index = await getOrCreateIndex(pinecone, INDEX_NAME)

// Get the namespace
const pineconeNamespace = index.namespace(namespace ?? '')
// console.log("embeddings", JSON.stringify(embeddings))

try {
// Query the index with the defined request
const queryResult = await pineconeNamespace.query({
vector: embeddings,
topK,
includeMetadata: true,
includeMetadata: true
})
return queryResult.matches || []
} catch (e) {
// Log the error and throw it
console.log("Error querying embeddings: ", e)
console.log('Error querying embeddings: ', e)
throw new Error(`Error querying embeddings: ${e}`)
}
}

export { getMatchesFromEmbeddings };

export { getMatchesFromEmbeddings }

0 comments on commit c08ebe7

Please sign in to comment.