這是一個直觀的異步Scala客戶端,用於Pinecone API,支持所有可用索引,矢量,收集,推理和助理操作/端點,以兩種便利服務提供,稱為PineconeVectorService和PineconeIndexSservice。受支持的電話是:
PineconeAssistantService和PineconeAssistantFileService請注意,為了與Pinecone API命名保持一致,服務函數名稱與API端點標題/描述與Camelcase完全匹配。另外,我們的目標是通過最少的依賴關係進行自我限制,因此我們最終僅使用兩個libs play-ahc-ws-standalone和play-ws-standalone-json (位於頂級)。
✔️重要:這是一個“社區維護”的圖書館,因此,與Pinecone公司無關。
在媒體上查看有關LIB/客戶端的文章。另外,如果您想立即查看動手示例,請轉到Pinecone示例或OpenAI + Pinecone示例模塊。
當前支持的Scala版本為2.12、2.13和3 。
要拉庫,您必須將以下依賴關係添加到構建中。
"io.cequence" %% "pinecone-scala-client" % "1.2.2"
或to pom.xml (如果使用maven)
<dependency>
<groupId>io.cequence</groupId>
<artifactId>pinecone-scala-client_2.12</artifactId>
<version>1.2.2</version>
</dependency>
PINECONE_SCALA_CLIENT_API_KEY ,如果使用了基於pod的服務, PINECONE_SCALA_CLIENT_ENV ia。獲得PineconeIndexService
首先,您需要提供隱式執行上下文以及Akka材料器,例如
implicit val ec = ExecutionContext .global
implicit val materializer = Materializer ( ActorSystem ())然後,您可以通過以下方式之一獲得服務(基於POD或無服務器)。
Config部分中定義) import io . cequence . pineconescala . service . PineconeIndexServiceFactory . FactoryImplicits
val service = PineconeIndexServiceFactory ().asOne val config = ConfigFactory .load( " path_to_my_custom_config " )
val service = PineconeIndexServiceFactory (config).asOnePineconePodBasedIndexService的實例 val service = PineconeIndexServiceFactory (
apiKey = " your_api_key " ,
environment = " your_env " // e.g. "northamerica-northeast1-gcp
)PineconeServerlessIndexService的實例 val service = PineconeIndexServiceFactory (
apiKey = " your_api_key "
) IB。獲得PineconeVectorService
與PineconeIndexService相同,您需要首先提供隱式執行上下文和Akka Materializer。然後,您可以通過以下方式之一獲得服務。
Config部分中的定義)。請注意,如果沒有給定名稱的索引,則工廠將None返回。 PineconeVectorServiceFactory ( " index_name " ).map { serviceOption =>
val service = serviceOption.getOrElse(
throw new Exception ( s " Index with a given name does not exist. " )
)
// do something with the service
}我知道了。獲得PineconeInferenceService
與PineconeIndexService相同,您需要首先提供隱式執行上下文和Akka Materializer。然後,您可以通過以下方式之一獲得服務。
val service = PineconeInferenceServiceFactory () val config = ConfigFactory .load( " path_to_my_custom_config " )
val service = PineconeInferenceServiceFactory (config) val service = PineconeInferenceServiceFactory (
apiKey = " your_api_key "
) ID。獲得PineconeAssistantService
val service = PineconeAssistantServiceFactory () val config = ConfigFactory .load( " path_to_my_custom_config " )
val service = PineconeAssistantServiceFactory (config) val service = PineconeAssistantServiceFactory (
apiKey = " your_api "
) IE。獲取PineconeAssistantFileService
val service = PineconeAssistantFileServiceFactory () val config = ConfigFactory .load( " path_to_my_custom_config " )
val service = PineconeAssistantFileServiceFactory (config) val service = PineconeAssistantFileServiceFactory (
apiKey = " your_api "
)ii。調用功能
PineconeVectorService和PineconeIndexService提供了每個呼叫及其各自的輸入和設置的完整文檔。由於所有呼叫都是異步的,因此他們返回Future包含的響應。
示例:
索引操作
pineconeIndexService.listIndexes.map(indexes =>
indexes.foreach(println)
) import io . cequence . pineconescala . domain . response . CreateResponse
pineconeIndexService.createIndex(
name = " auto-gpt-test " ,
dimension = 1536
).map {
case CreateResponse . Created => println( " Index successfully created. " )
case CreateResponse . BadRequest => println( " Index creation failed. Request exceeds quota or an invalid index name. " )
case CreateResponse . AlreadyExists => println( " Index with a given name already exists. " )
} pineconeIndexService.describeIndex( " index_name " ).map(indexInfo =>
// if not found, indexInfo will be None
println(indexInfo)
) import io . cequence . pineconescala . domain . response . DeleteResponse
pineconeIndexService.deleteIndex( " index_name " ).map {
case DeleteResponse . Deleted => println( " Index successfully deleted. " )
case DeleteResponse . NotFound => println( " Index with a given name not found. " )
} import io . cequence . pineconescala . domain . response . ConfigureIndexResponse
pineconeIndexService.configureIndex(
name = " index_name " ,
replicas = Some ( 2 ),
pod_type = Some ( PodType .p1_x2)
).map {
case ConfigureIndexResponse . Updated => println( " Index successfully updated. " )
case ConfigureIndexResponse . BadRequestNotEnoughQuota => println( " Index update failed. Not enough quota. " )
case ConfigureIndexResponse . NotFound => println( " Index with a given name not found. " )
}收集操作
pineconeIndexService.listCollections.map(collectionNames =>
println(collectionNames.mkString( " , " ))
) import io . cequence . pineconescala . domain . response . CreateResponse
pineconeIndexService.createCollection(
name = " collection_name " ,
source = " index_name "
).map {
case CreateResponse . Created => println( " Collection successfully created. " )
case CreateResponse . BadRequest => println( " Collection creation failed. Request exceeds quota or an invalid collection name. " )
case CreateResponse . AlreadyExists => println( " Collection with a given name already exists. " )
} pineconeIndexService.describeCollection( " collection_name " ).map(collectionInfo =>
// if not found, collectionInfo will be None
println(collectionInfo)
) import io . cequence . pineconescala . domain . response . DeleteResponse
pineconeIndexService.deleteCollection( " collection_name " ).map {
case DeleteResponse . Deleted => println( " Collection successfully deleted. " )
case DeleteResponse . NotFound => println( " Collection with a given name not found. " )
}向量操作
val dimension = 1536
pineconeVectorService.upsert(
vectors = Seq (
PVector (
id = " 666 " ,
values = Seq .fill(dimension)( Random .nextDouble),
metadata = Map (
" is_relevant " -> " not really but for testing it's ok, you know " ,
" food_quality " -> " brunches are perfect but don't go there before closing time "
)
),
PVector (
id = " 777 " ,
values = Seq .fill(dimension)( Random .nextDouble),
metadata = Map (
" is_relevant " -> " very much so " ,
" food_quality " -> " burritos are the best! "
)
)
),
namespace = " my_namespace " ,
).map(vectorUpsertedCount =>
println( s " Upserted $vectorUpsertedCount vectors. " )
) val fetchedValues = ... // vectors fetched from somewhere
pineconeVectorService.update(
id = " 777 " ,
namespace = " my_namespace " ,
values = fetchedValues.map(_ / 100 ), // divide fetched values by 100
sparseValues = Some ( SparseVector (
indices = Seq ( 1 , 2 , 3 ),
values = Seq ( 8.8 , 7.7 , 2.2 )
)),
setMetaData = Map (
" solid_info " -> " this is the source of the truth "
)
).map(_ =>
println( s " Vectors updated. " )
) pineconeVectorService.query(
vector = Seq .fill( 1536 )( Random .nextDouble), // some values/embeddings
namespace = " my_namespace "
).map { queryResponse =>
queryResponse.matches.foreach { matchInfo =>
println( s " Matched vector id: ${matchInfo.id} " )
println( s " Matched vector values: ${matchInfo.values.take( 20 ).mkString( " , " )} .. " )
println( s " Matched vector score: ${matchInfo.score} " )
println( s " Matched vector metadata: ${matchInfo.metadata} " )
}
} pineconeVectorService.query(
vector = Seq .fill( 1536 )( Random .nextDouble), // some values/embeddings
namespace = " my_namespace " ,
settings = QuerySettings (
topK = 5 ,
includeValues = true ,
includeMetadata = true
)
).map { queryResponse =>
queryResponse.matches.foreach { matchInfo =>
println( s " Matched vector id: ${matchInfo.id} " )
println( s " Matched vector values: ${matchInfo.values.take( 20 ).mkString( " , " )} .. " )
println( s " Matched vector score: ${matchInfo.score} " )
println( s " Matched vector metadata: ${matchInfo.metadata} " )
}
} pineconeVectorService.fetch(
ids = Seq ( " 666 " , " 777 " ),
namespace = " my_namespace "
).map { fetchResponse =>
fetchResponse.vectors.values.map { pVector =>
println( s " Fetched vector id: ${pVector.id} " )
println( s " Fetched vector values: ${pVector.values.take( 20 ).mkString( " , " )} .. " )
println( s " Fetched vector metadata: ${pVector.metadata} " )
}
} pineconeVectorService.delete(
ids = Seq ( " 666 " , " 777 " ),
namespace = " my_namespace "
).map(_ =>
println( " Vectors deleted " )
) pineconeVectorService.deleteAll(
namespace = " my_namespace "
).map(_ =>
println( " All vectors deleted " )
) pineconeVectorService.describeIndexStats.map(stats =>
println(stats)
)推理操作
pineconeInferenceService.createEmbeddings(
Seq ( " The quick brown fox jumped over the lazy dog " )
).map { embeddings =>
println(embeddings.data.mkString( " n " ))
} pineconeInferenceService.rerank(
query = " The tech company Apple is known for its innovative products like the iPhone. " ,
documents = Seq (...)
).map(
_.data.foreach(println)
) pineconeInferenceService.evaluate(
question = " What are the capital cities of France, England and Spain? " ,
answer = " Paris is a city of France and Barcelona of Spain " ,
groundTruthAnswer = " Paris is the capital city of France, London of England and Madrid of Spain "
).map { response =>
println(response)
}**助理行動**
pineconeAssistantService.listAssistants.map(assistants =>
println(assistants.mkString( " , " ))
) import io . cequence . pineconescala . domain . response . CreateResponse
pineconeAssistantService.createAssistant(
name = " assistant_name " ,
description = " assistant_description " ,
assistantType = " assistant_type "
).map {
case CreateResponse . Created => println( " Assistant successfully created. " )
case CreateResponse . BadRequest => println( " Assistant creation failed. Request exceeds quota or an invalid assistant name. " )
case CreateResponse . AlreadyExists => println( " Assistant with a given name already exists. " )
} pineconeAssistantService.describeAssistant( " assistant_name " ).map(assistant =>
// if not found, assistant will be None
println(assistant)
) import io . cequence . pineconescala . domain . response . DeleteResponse
pineconeAssistantService.deleteAssistant( " assistant_name " ).map {
case DeleteResponse . Deleted => println( " Assistant successfully deleted. " )
case DeleteResponse . NotFound => println( " Assistant with a given name not found. " )
} pineconeAssistantService.listFiles( " assistant_name " ).map(files =>
println(files.mkString( " , " ))
) import io . cequence . pineconescala . domain . response . CreateResponse
pineconeAssistantService.uploadFile(
assistantName = " assistant_name " ,
filePath = " path_to_file "
).map {
case CreateResponse . Created => println( " File successfully uploaded. " )
case CreateResponse . BadRequest => println( " File upload failed. Request exceeds quota or an invalid file path. " )
case CreateResponse . AlreadyExists => println( " File with a given name already exists. " )
} pineconeAssistantService.describeFile( " assistant_name " , " file_name " ).map(file =>
// if not found, file will be None
println(file)
) pineconeAssistantService.chatWithAssistant(
" assistant_name " ,
" What is the maximum height of a red pine? "
).map(response =>
println(response)
)對於即將運行的演示請求。請參閱單獨的模塊:
我有一個超時例外。如何更改超時設置?
您可以通過將timeouts傳遞給Pinecone{Vector,Index}ServiceFactory來做到這一點,或者,如果您使用自己的配置文件,則可以簡單地將其添加為:
pinecone-scala-client {
timeouts {
requestTimeoutSec = 200
readTimeoutSec = 200
connectTimeoutSec = 5
pooledConnectionIdleTimeoutSec = 60
}
}
我得到了一個例外,例如com.typesafe.config.ConfigException$UnresolvedSubstitution: pinecone-scala-client.conf @ jar:file:.../io/cequence/pinecone-scala-client_2.13/1.2.2/pinecone-scala-client_2.13-1.2.2.jar!/pinecone-scala-client.conf: 4: Could not resolve substitution to a value: ${PINECONE_SCALA_CLIENT_API_KEY} 。我應該怎麼辦?
設置Env。變量PINECONE_SCALA_CLIENT_API_KEY 。如果您在這裡沒有一個登記冊。
一切看起來都很酷。我想與您聊天有關您的研發?
只需通過[email protected]向我們發送電子郵件。
根據MIT許可條款,該庫可用並以開源為開源。
該項目是開源的,歡迎任何貢獻或反饋(在此)。
該圖書館的開發得到了 - cequence.io的支持 - The future of contracting
由彼得·班達(Peter Banda)創建和維護。