packageio.weaviate; importio.weaviate.client.Config; importio.weaviate.client.WeaviateClient; importio.weaviate.client.base.Result; importio.weaviate.client.v1.graphql.model.GraphQLResponse; importio.weaviate.client.v1.graphql.query.argument.NearObjectArgument; importio.weaviate.client.v1.graphql.query.fields.Field; publicclassApp{ publicstaticvoidmain(String[] args){ Config config =newConfig("http","localhost:8080"); WeaviateClient client =newWeaviateClient(config); String className ="Publication"; Field name =Field.builder().name("name").build(); Field _additional =Field.builder() .name("_additional") .fields(newField[]{ Field.builder().name("certainty").build(),// only supported if distance==cosine Field.builder().name("distance").build()// always supported }).build(); NearObjectArgument nearObject = client.graphQL().arguments().nearObjectArgBuilder() .id("32d5a368-ace8-3bb7-ade7-9f7ff03eddb6") .build(); Result<GraphQLResponse> result = client.graphQL().get() .withClassName(className) .withFields(name, _additional) .withNearObject(nearObject) .run(); if(result.hasErrors()){ System.out.println(result.getError()); return; } System.out.println(result.getResult()); } }
# Note: prior to v1.14, use `certainty` instead of `distance` # Under _additional, `certainty` is only supported if distance==cosine, but `distance` is always supported echo '{ "query": "{ Get { Publication( nearObject: { id: \"32d5a368-ace8-3bb7-ade7-9f7ff03eddb6\", distance: 0.6 } ) { name _additional { certainty distance } } } }" }' | curl \ -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer learn-weaviate' \ -d @- \ https://edu-demo.weaviate.network/v1/graphql
{ Get{ Publication( nearObject:{ id:"32d5a368-ace8-3bb7-ade7-9f7ff03eddb6",# or weaviate:///32d5a368-ace8-3bb7-ade7-9f7ff03eddb6 distance:0.6# prior to v1.14, use certainty: 0.7 } ){ name _additional{ certainty# only works if distance==cosine distance# always works } } } }
预期响应
{ "data":{ "Get":{ "Publication":[ { "_additional":{ "distance":-1.1920929e-07 }, "name":"The New York Times Company" }, { "_additional":{ "distance":0.059879005 }, "name":"New York Times" }, { "_additional":{ "distance":0.09176409 }, "name":"International New York Times" }, { "_additional":{ "distance":0.13954824 }, "name":"New Yorker" }, ... ] } } }
import weaviate import weaviate.classes as wvc import os client = weaviate.connect_to_local( headers={ "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") } ) try: collection = client.collections.use("Article") response = collection.query.near_text( query="travelling in Asia", certainty=0.7, move_to=wvc.query.Move( force=0.75, objects="c4209549-7981-3699-9648-61a78c2124b9" ), return_metadata=wvc.query.MetadataQuery(certainty=True), limit=5, ) for o in response.objects: print(o.properties) print(o.metadata.certainty) finally: client.close()
package main import( "context" "fmt" "github.com/weaviate/weaviate-go-client/v5/weaviate" "github.com/weaviate/weaviate-go-client/v5/weaviate/graphql" ) funcmain(){ cfg := weaviate.Config{ Host:"localhost:8080", Scheme:"http", } client, err := weaviate.NewClient(cfg) if err !=nil{ panic(err) } className :="Article" title := graphql.Field{Name:"title"} summary := graphql.Field{Name:"summary"} _additional := graphql.Field{ Name:"_additional", Fields:[]graphql.Field{ {Name:"certainty"}, }, } concepts :=[]string{"travelling in Asia"} certainty :=float32(0.7) moveTo :=&graphql.MoveParameters{ Objects:[]graphql.MoverObject{ // this ID is of the article: "Tohoku: A Japan destination for all seasons." {ID:"c4209549-7981-3699-9648-61a78c2124b9"}, }, Force:0.85, } nearText := client.GraphQL().NearTextArgBuilder(). WithConcepts(concepts). WithCertainty(certainty). WithMoveTo(moveTo) ctx := context.Background() result, err := client.GraphQL().Get(). WithClassName(className). WithFields(title, summary, _additional). WithNearText(nearText). Do(ctx) if err !=nil{ panic(err) } fmt.Printf("%v", result) }
packageio.weaviate; importio.weaviate.client.Config; importio.weaviate.client.WeaviateClient; importio.weaviate.client.base.Result; importio.weaviate.client.v1.graphql.model.GraphQLResponse; importio.weaviate.client.v1.graphql.query.argument.NearTextArgument; importio.weaviate.client.v1.graphql.query.argument.NearTextMoveParameters; importio.weaviate.client.v1.graphql.query.fields.Field; publicclassApp{ publicstaticvoidmain(String[] args){ Config config =newConfig("http","localhost:8080"); WeaviateClient client =newWeaviateClient(config); NearTextMoveParameters moveTo =NearTextMoveParameters.builder() .objects(newNearTextMoveParameters.ObjectMove[]{ // this ID is of the article: "Tohoku: A Japan destination for all seasons." NearTextMoveParameters.ObjectMove.builder().id("c4209549-7981-3699-9648-61a78c2124b9").build() }) .force(0.85f) .build(); NearTextArgument nearText = client.graphQL().arguments().nearTextArgBuilder() .concepts(newString[]{"travelling in Asia"}) .certainty(0.7f) .moveTo(moveTo) .build(); Field title =Field.builder().name("title").build(); Field summary =Field.builder().name("summary").build(); Field _additional =Field.builder() .name("_additional") .fields(newField[]{ Field.builder().name("certainty").build(), }).build(); Result<GraphQLResponse> result = client.graphQL().get() .withClassName("Article") .withFields(title, summary, _additional) .withNearText(nearText) .run(); if(result.hasErrors()){ System.out.println(result.getError()); return; } System.out.println(result.getResult()); } }
# The ID belongs to the article "Tohoku: A Japan destination for all seasons." echo '{ "query": "{ Get { Article( nearText: { concepts: [\"travelling in Asia\"], certainty: 0.7, moveTo: { objects: [{ id: \"c4209549-7981-3699-9648-61a78c2124b9\" }] force: 0.85 } } ) { title summary _additional { certainty } } } }" }' | curl \ -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer learn-weaviate' \ -H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \ -d @- \ https://edu-demo.weaviate.network/v1/graphql
{ Get{ Article( nearText:{ concepts:["travelling in Asia"], certainty:0.7, moveTo:{ objects:[{ # this ID is of the article: # "Tohoku: A Japan destination for all seasons." id:"c4209549-7981-3699-9648-61a78c2124b9" }] force:0.85 } } ){ title summary _additional{ certainty } } } }
预期响应
{ "data":{ "Get":{ "Article":[ { "_additional":{ "certainty":0.9619976580142975 }, "summary":"We've scoured the planet for what we think are 50 of the most delicious foods ever created. A Hong Kong best food, best enjoyed before cholesterol checks. When you have a best food as naturally delicious as these little fellas, keep it simple. Courtesy Matt@PEK/Creative Commons/FlickrThis best food Thai masterpiece teems with shrimp, mushrooms, tomatoes, lemongrass, galangal and kaffir lime leaves. It's a result of being born in a land where the world's most delicious food is sold on nearly every street corner.", "title":"World food: 50 best dishes" }, { "_additional":{ "certainty":0.9297388792037964 }, "summary":"The look reflects the elegant ambiance created by interior designer Joyce Wang in Hong Kong, while their mixology program also reflects the original venue. MONO Hong Kong , 5/F, 18 On Lan Street, Central, Hong KongKoral, The Apurva Kempinski Bali, IndonesiaKoral's signature dish: Tomatoes Bedugul. Esterre at Palace Hotel TokyoLegendary French chef Alain Ducasse has a global portfolio of restaurants, many holding Michelin stars. John Anthony/JW Marriott HanoiCantonese cuisine from Hong Kong is again on the menu, this time at the JW Marriott in Hanoi. Stanley takes its name from the elegant Hong Kong waterside district and the design touches reflect this legacy with Chinese antiques.", "title":"20 best new Asia-Pacific restaurants to try in 2020" } ... ] } } }
import weaviate import weaviate.classes as wvc import os client = weaviate.connect_to_local( headers={ "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") } ) try: collection = client.collections.use("Article") response = collection.query.hybrid( query="Fisherman that catches salmon", alpha=0.5, return_metadata=wvc.query.MetadataQuery(score=True, explain_score=True), limit=5, ) for o in response.objects: print(o.properties) print(o.metadata.score) print(o.metadata.explain_score) finally: client.close()
hybrid :=&HybridArgumentBuilder{} hybrid.WithQuery("Fisherman that catches salmon").WithAlpha(0.5) query := builder.WithClassName("Article").WithHybrid(hybrid).build()
HybridArgument hybrid = client.graphQL().arguments().HybridArgBuilder() .query("Fisherman that catches salmon") .alpha(0.5f) .build(); Field name =Field.builder().name("title""summary").build(); Field _additional =Field.builder() .name("_additional") .fields(newField[]{Field.builder().name("score explainScore").build()}) .build(); // when testGenerics.createTestSchemaAndData(client); Result<GraphQLResponse> result = client.graphQL().get().withClassName("Article") .withHybrid(hybrid) .withFields(name, _additional).run();
import weaviate import weaviate.classes as wvc import os client = weaviate.connect_to_local( headers={ "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") } ) try: collection = client.collections.use("Article") response = collection.query.hybrid( query="Fisherman that catches salmon", vector=query_vector, alpha=0.5, return_metadata=wvc.query.MetadataQuery(score=True, explain_score=True), limit=5, ) for o in response.objects: print(o.properties) print(o.metadata.score) print(o.metadata.explain_score) finally: client.close()
hybrid :=&HybridArgumentBuilder{} hybrid.WithQuery("Fisherman that catches salmon").WithVector(1,2,3).WithAlpha(0.5) query := builder.WithClassName("Article").WithHybrid(hybrid).build()
HybridArgument hybrid = client.graphQL().arguments().HybridArgBuilder() .query("Fisherman that catches salmon") .vector(Float[]{1f,2f,3f}) .alpha(0.5f) .build(); Field name =Field.builder().name("title""summary").build(); Field _additional =Field.builder() .name("_additional") .fields(newField[]{Field.builder().name("score").build()}) .build(); // when testGenerics.createTestSchemaAndData(client); Result<GraphQLResponse> result = client.graphQL().get().withClassName("Article") .withHybrid(hybrid) .withFields(name, _additional).run();
# The `vector` below is optional. Not needed if Weaviate handles the vectorization. # If you provide your own embeddings, put the vector query there, and make sure it has the correct number of dimensions. echo '{ "query": "{ Get { Article( hybrid: { query: \"Fisherman that catches salmon\" alpha: 0.5 vector: [1, 2, 3] }) { title summary _additional { score } } } }" }' | curl \ -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer learn-weaviate' \ -H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \ -d @- \ https://edu-demo.weaviate.network/v1/graphql
{ Get{ Article( hybrid:{ query:"Fisherman that catches salmon" alpha:0.5 vector:[1,2,3]# optional. Not needed if Weaviate handles the vectorization. If you provide your own embeddings, put the vector query here. }) { title summary _additional{score} } } }
import weaviate import weaviate.classes as wvc import os client = weaviate.connect_to_local( headers={ "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") } ) try: collection = client.collections.use("Article") response = collection.query.hybrid( query="How to catch an Alaskan Pollock", alpha=0.5, filters=wvc.query.Filter.by_property("wordCount").less_than(1000), limit=5, ) for o in response.objects: print(o.properties) finally: client.close()
where := filters.Where(). WithPath([]string{"content"}). WithOperator(filters.Equal). WithValueString("Alaskan")// All results must have "Alaskan" in the content property name = graphql.Field{Name:"summary"} hybrid :=&graphql.HybridArgumentBuilder{} hybrid.WithQuery("How to catch an Alaskan Pollock").WithAlpha(0.5) resultSet, gqlErr := client.GraphQL().Get().WithClassName("Article").WithHybrid(hybrid).WithWhere(where).WithFields(name).Do(context.Background()) articles := get["Article"].([]interface{})
Field title =Field.builder().name("title""summary").build(); WhereFilter where =WhereFilter.builder() .path(newString[]{"wordCount"}) .operator(Operator.LessThan) .valueInt(1000) .build(); HybridFilter hybridFilter =HybridFilter.builder() .query("How to catch an Alaskan Pollock.") .alpha(0.5) .build(); Result<GraphQLResponse> result = client.graphQL().get() .withClassName("Article") .withFields(title) .withWhere(where) .withHybrid(hybridFilter) .run();
import weaviate import weaviate.classes as wvc import os client = weaviate.connect_to_local( headers={ "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") } ) try: collection = client.collections.use("JeopardyQuestion") response = collection.query.hybrid( query="Venus", alpha=0.25, query_properties=["question"], return_metadata=wvc.query.MetadataQuery(score=True), limit=5, ) for o in response.objects: print(o.properties) print(o.metadata.score) finally: client.close()
HybridArgument hybrid = client.graphQL().arguments().HybridArgBuilder() .query("Fisherman that catches salmon") .alpha(0.25f)// closer to pure keyword search .properties(String[]{"question"})// changing to "answer" will yield a different result set .build(); Field name =Field.builder().name("question""answer").build(); Field _additional =Field.builder() .name("_additional") .fields(newField[]{Field.builder().name("score").build()}) .build(); // when testGenerics.createTestSchemaAndData(client); Result<GraphQLResponse> result = client.graphQL().get().withClassName("JeopardyQuestion") .withHybrid(hybrid) .withFields(name, _additional) .withLimit(3) .run();
{ Get{ JeopardyQuestion( hybrid:{ query:"Venus" alpha:0.25# closer to pure keyword search properties:["question"]# changing to "answer" will yield a different result set } limit:3 ){ question answer _additional{ score } } } }
{ "data":{ "Get":{ "Article":[ { "_additional":{ "certainty":null, "distance":null, "score":"3.4985464" }, "title":"Tim Dowling: is the dog’s friendship with the fox sweet – or a bad omen?" } ] } }, "errors":null }
{ Get{ Article( bm25:{query:"how to fish",properties:["title"]} where:{path:["wordCount"],operator:LessThan,valueInt:1000} ){ summary title } } }
预期响应
{ "data":{ "Get":{ "Article":[ { "summary":"Sometimes, the hardest part of setting a fishing record is just getting the fish weighed. A Kentucky fisherman has officially set a new record in the state after reeling in a 9.05-pound saugeye. While getting the fish in the boat was difficult, the angler had just as much trouble finding an officially certified scale to weigh it on. In order to qualify for a state record, fish must be weighed on an officially certified scale. The previous record for a saugeye in Kentucky ws an 8 pound, 8-ounce fish caught in 2019.", "title":"Kentucky fisherman catches record-breaking fish, searches for certified scale" }, { "summary":"Unpaid last month because there wasn\u2019t enough money. Ms. Hunt picks up shifts at JJ Fish & Chicken, bartends and babysits. three daughters is subsidized,and cereal fromErica Hunt\u2019s monthly budget on $12 an hourErica Hunt\u2019s monthly budget on $12 an hourExpensesIncome and benefitsRent, $775Take-home pay, $1,400Varies based on hours worked. Daycare, $600Daycare for Ms. Hunt\u2019s three daughters is subsidized, as are her electricity and internet costs. Household goods, $300Child support, $350Ms. Hunt picks up shifts at JJ Fish & Chicken, bartends and babysits to make more money.", "title":"Opinion | What to Tell the Critics of a $15 Minimum Wage" }, ... ] } } }
import weaviate import weaviate.classes as wvc import os client = weaviate.connect_to_local( headers={ "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") } ) try: # QnA module use is not yet supported by the V4 client. Please use a raw GraphQL query instead. response = client.graphql_raw_query( """ { Get { Article( ask: { question: "Who is the king of the Netherlands?", properties: ["summary"], }, limit: 1 ) { title _additional { answer { hasAnswer property result startPosition endPosition } } } } } """ finally: client.close()
package main import( "context" "fmt" "github.com/weaviate/weaviate-go-client/v5/weaviate" "github.com/weaviate/weaviate-go-client/v5/weaviate/graphql" ) funcmain(){ cfg := weaviate.Config{ Host:"localhost:8080", Scheme:"http", } client, err := weaviate.NewClient(cfg) if err !=nil{ panic(err) } className :="Article" fields :=[]graphql.Field{ {Name:"title"}, {Name:"_additional", Fields:[]graphql.Field{ {Name:"answer", Fields:[]graphql.Field{ {Name:"hasAnswer"}, {Name:"certainty"}, {Name:"property"}, {Name:"result"}, {Name:"startPosition"}, {Name:"endPosition"}, }}, }}, } ask := client.GraphQL().AskArgBuilder(). WithQuestion("Who is the king of the Netherlands?"). WithProperties([]string{"summary"}) ctx := context.Background() result, err := client.GraphQL().Get(). WithClassName(className). WithFields(fields...). WithAsk(ask). WithLimit(1). Do(ctx) if err !=nil{ panic(err) } fmt.Printf("%v", result) }
packageio.weaviate; importio.weaviate.client.Config; importio.weaviate.client.WeaviateClient; importio.weaviate.client.base.Result; importio.weaviate.client.v1.graphql.model.GraphQLResponse; importio.weaviate.client.v1.graphql.query.argument.AskArgument; importio.weaviate.client.v1.graphql.query.fields.Field; publicclassApp{ publicstaticvoidmain(String[] args){ Config config =newConfig("http","localhost:8080"); WeaviateClient client =newWeaviateClient(config); Field title =Field.builder().name("title").build(); Field _additional =Field.builder() .name("_additional") .fields(newField[]{ Field.builder() .name("answer") .fields(newField[]{ Field.builder().name("hasAnswer").build(), Field.builder().name("certainty").build(), Field.builder().name("property").build(), Field.builder().name("result").build(), Field.builder().name("startPosition").build(), Field.builder().name("endPosition").build() }).build() }).build(); AskArgument ask =AskArgument.builder() .question("Who is the king of the Netherlands?") .properties(newString[]{"summary"}) .build(); Result<GraphQLResponse> result = client.graphQL().get() .withClassName("Article") .withFields(title, _additional) .withAsk(ask) .withLimit(1) .run(); if(result.hasErrors()){ System.out.println(result.getError()); return; } System.out.println(result.getResult()); } }
echo '{ "query": "{ Get { Article( ask: { question: \"Who is the king of the Netherlands?\", properties: [\"summary\"] }, limit: 1 ) { title _additional { answer { hasAnswer property result startPosition endPosition } } } } } " }' | curl \ -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer learn-weaviate' \ -H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \ -d @- \ https://edu-demo.weaviate.network/v1/graphql
{ Get{ Article( ask:{ question:"Who is the king of the Netherlands?", properties:["summary"], }, limit:1 ){ title _additional{ answer{ hasAnswer property result startPosition endPosition } } } } }