これは、PineconeVectorserviceとPineconeIndexServiceと呼ばれる2つの便利なサービスで提供されるすべての利用可能なインデックス、ベクトル、コレクション、推論、およびエンドポイントをサポートするPinecone API用の直感的な非薄型SCALAクライアントです。サポートされている呼び出しは次のとおりです。
PineconeAssistantServiceとPineconeAssistantFileService 2つのサービスによって提供されます。 Pinecone APIネーミングと一致するために、サービス関数名はAPIエンドポイントのタイトル/説明とCamelCaseと正確に一致することに注意してください。また、LIBは、可能な限り少ない依存関係で自己完結型であることを目指しました。したがって、最終的には2つのLIBS play-ahc-ws-standaloneとplay-ws-standalone-json (トップレベル)のみを使用しました。
重要:これは「コミュニティに維持されている」ライブラリであり、そのため、Pinecone Companyとは関係ありません。
MediumのLIB/クライアントに関する記事をご覧ください。また、すぐに実践的な例を見たい場合は、Pineconeの例またはOpenai + Pineconeの例にアクセスしてください。
現在サポートされているSCALAバージョンは2.12、2.13 、および3です。
ライブラリを引くには、 build.sbtに次の依存関係を追加する必要があります。
"io.cequence" %% "pinecone-scala-client" % "1.2.2"
または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 、およびPINECONE_SCALA_CLIENT_ENVポッドベースのサービスが使用されている場合 ia。 PineconeIndexServiceの取得
まず、暗黙の実行コンテキストとAkka Materalizerを提供する必要があります。
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 Materalizerを提供する必要があります。次に、次の方法のいずれかでサービスを取得できます。
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
} IC。 PineconeInferenceServiceの取得
PineconeIndexServiceと同じように、最初に暗黙の実行コンテキストとAkka Materalizerを提供する必要があります。次に、次の方法のいずれかでサービスを取得できます。
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 "
)すなわち。 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)
)すぐに実行できるデモplsの場合。個別のモジュールを参照してください。
タイムアウトの例外がありました。タイムアウト設定を変更するにはどうすればよいですか?
timeouts ParamをPinecone{Vector,Index}ServiceFactoryに渡すか、独自の構成ファイルを使用する場合は、次のように追加できます。
pinecone-scala-client {
timeouts {
requestTimeoutSec = 200
readTimeoutSec = 200
connectTimeoutSec = 5
pooledConnectionIdleTimeoutSec = 60
}
}
I got an exception like 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 。ここに1つの登録がない場合。
それはすべてクールに見えます。私はあなたの研究開発についてあなたとチャットしたいですか?
[email protected]でメールを撮影してください。
このライブラリは、MITライセンスの条件の下でオープンソースとして利用可能であり、公開されています。
このプロジェクトはオープンソースであり、貢献やフィードバックを歓迎します(こちら)。
このライブラリの開発は-cequence.io- The future of contractingによってサポートされています
Peter Bandaによって作成および維持されています。