client

package module
v0.0.0-...-2c5e70e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2023 License: Apache-2.0 Imports: 20 Imported by: 0

README

Jina Golang Client

Install
go get github.com/jina-ai/client-go
Basic Usage
package main

import (
	"fmt"

	"github.com/jina-ai/client-go"
	"github.com/jina-ai/client-go/docarray"
	"github.com/jina-ai/client-go/jina"
)

// Create a Document
func getDoc(id string) *docarray.DocumentProto {
	return &docarray.DocumentProto{
		Id: id,
		Content: &docarray.DocumentProto_Text{
			Text: "Hello world. This is a test document with id:" + id,
		},
	}
}

// Create a DocumentArray with 3 Documents
func getDocarray() *docarray.DocumentArrayProto {
	return &docarray.DocumentArrayProto{
		Docs: []*docarray.DocumentProto{getDoc("1"), getDoc("2"), getDoc("3")},
	}
}

// Create DataRequest with a DocumentArray
func getDataRequest() *jina.DataRequestProto {
	return &jina.DataRequestProto{
		Data: &jina.DataRequestProto_DataContentProto{
			Documents: &jina.DataRequestProto_DataContentProto_Docs{
				Docs: getDocarray(),
			},
		},
	}
}

// Generate `DataRequest`s with random DocumentArrays
func generateDataRequests() <-chan *jina.DataRequestProto {
	requests := make(chan *jina.DataRequestProto)
	go func() {
		// Generate 10 requests
		for i := 0; i < 10; i++ {
			requests <- getDataRequest()
		}
		defer close(requests)
	}()
	return requests
}

// Custom OnDone callback
func OnDone(resp *jina.DataRequestProto) {
	fmt.Println("Got a successful response!")
}

// Custom OnError callback
func OnError(resp *jina.DataRequestProto) {
	fmt.Println("Got an error in response!")
}

func main() {
    	// Create a HTTP client (expects a Jina Flow with http protocol running on localhost:12345)
	HTTPClient, err := client.NewHTTPClient("http://localhost:12345")
	if err != nil {
		panic(err)
	}
    
    	// Send requests to the Flow
	HTTPClient.POST(generateDataRequests(), OnDone, OnError, nil)
}

Examples
Example
gRPC Stream requests using gRPC Client
HTTP Stream requests using HTTP Client
WebSocket Stream requests using WebSocket Client
gRPC Healthcheck Check if the gRPC Flow is healthy
HTTP Healthcheck Check if the HTTP Flow is healthy
WebSocket Healthcheck Check if the WebSocket Flow is healthy
gRPC Info Get info about the gRPC Flow
HTTP Info Get info about the HTTP Flow
WebSocket Info Get info about the WebSocket Flow
DocArray usage Example usage of DocArray (TODO)
Concurrent requests Send concurrent requests to Jina Gateway (TODO)
Gotchas
Directory structure
.
├── protos
│   ├── docarray.proto          # proto file for DocArray
│   └── jina.proto              # proto file for Jina
├── docarray                    # docarray package
│   ├── docarray.pb.go          # generated from docarray.proto  
│   └── json.go                 # custom json (un)marshaler for few fields in docarray.proto
├── jina                        # jina package
│   ├── jina_grpc.pb.go         # generated from jina.proto
│   ├── jina.pb.go              # generated from jina.proto
│   └── json.go                 # custom json (un)marshaler for few fields in jina.proto
├── client.go                   # Client interface
├── grpc.go                     # gRPC client
├── http.go                     # HTTP client
├── websocket.go                # WebSocket client
├── scripts
└   └── protogen.sh             # script to Golang code from proto files
  • scripts/protogen.sh generates the Golang code from the protos. Each proto generates code in a separate package. This is to avoid name clashes.

  • jina/json.go and docarray/json.go are custom json (un)marshalers for few fields in jina.proto and docarray.proto respectively.

  • client.go defines the Client & HealthCheckClient interface. This is implemented by grpc.go, http.go and websocket.go.

Jina/Docarray Version Compatibility

Current jina version is mentioned in the requirements.txt file. Every time, there's a PR that bumps the jina version (managed using Dependabot) , Version Update workflow,

  • Downloads the right protos for jina & docarray.
  • Generates the Golang code from the protos.
  • Runs integration tests.
  • If all tests pass, it commits the latest code into the same branch.

Once the PR is merged, a release is created with the new Jina version via Tag & Release workflow.


Another (better?) approach for keeping all docarray/jina versions compatible would be,

  • For all docarray versions, generate the Golang code from the protos under docarray/v1, docarray/v2 packages.
  • For all jina versions, generate the Golang code from the protos under jina/v1, jina/v2 packages.
  • Skip re-releasing the client-go package for every jina version, rather user can pick the right version of jina/docarray package.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HttpClient *http.Client
View Source
var Resolver = dnscache.New(10 * time.Second)

Functions

This section is empty.

Types

type CallbackType

type CallbackType func(*jina.DataRequestProto)

type Client

type Client interface {
	POST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error
	SequentialPOST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error
}

type GRPCClient

type GRPCClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewGRPCClient

func NewGRPCClient(host string) (*GRPCClient, error)

func (GRPCClient) POST

func (c GRPCClient) POST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error

func (GRPCClient) SequentialPOST

func (c GRPCClient) SequentialPOST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error

type GRPCHealthCheckClient

type GRPCHealthCheckClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewGRPCHealthCheckClient

func NewGRPCHealthCheckClient(host string) (*GRPCHealthCheckClient, error)

func (*GRPCHealthCheckClient) HealthCheck

func (c *GRPCHealthCheckClient) HealthCheck() (bool, error)

type GRPCInfoClient

type GRPCInfoClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewGRPCInfoClient

func NewGRPCInfoClient(host string) (*GRPCInfoClient, error)

func (*GRPCInfoClient) Info

func (c *GRPCInfoClient) Info() (*jina.JinaInfoProto, error)

func (*GRPCInfoClient) InfoJSON

func (c *GRPCInfoClient) InfoJSON() ([]byte, error)

type HTTPClient

type HTTPClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewHTTPClient

func NewHTTPClient(host string) (*HTTPClient, error)

func (HTTPClient) POST

func (c HTTPClient) POST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error

func (HTTPClient) SequentialPOST

func (c HTTPClient) SequentialPOST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error

type HTTPHealthCheckClient

type HTTPHealthCheckClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewHTTPHealthCheckClient

func NewHTTPHealthCheckClient(host string) (*HTTPHealthCheckClient, error)

func (HTTPHealthCheckClient) HealthCheck

func (c HTTPHealthCheckClient) HealthCheck() (bool, error)

type HTTPInfoClient

type HTTPInfoClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewHTTPInfoClient

func NewHTTPInfoClient(host string) (HTTPInfoClient, error)

func (HTTPInfoClient) Info

func (c HTTPInfoClient) Info() (*jina.JinaInfoProto, error)

func (HTTPInfoClient) InfoJSON

func (c HTTPInfoClient) InfoJSON() ([]byte, error)

type HealthCheckClient

type HealthCheckClient interface {
	HealthCheck() (bool, error)
}

type WebSocketClient

type WebSocketClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewWebSocketClient

func NewWebSocketClient(host string) (*WebSocketClient, error)

func (WebSocketClient) POST

func (client WebSocketClient) POST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error

func (WebSocketClient) SequentialPOST

func (client WebSocketClient) SequentialPOST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error

type WebSocketHealthCheckClient

type WebSocketHealthCheckClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewWebSocketHealthCheckClient

func NewWebSocketHealthCheckClient(host string) (*WebSocketHealthCheckClient, error)

func (WebSocketHealthCheckClient) HealthCheck

func (c WebSocketHealthCheckClient) HealthCheck() (bool, error)

type WebSocketInfoClient

type WebSocketInfoClient struct {
	Host string
	// contains filtered or unexported fields
}

func NewWebSocketInfoClient

func NewWebSocketInfoClient(host string) (WebSocketInfoClient, error)

func (WebSocketInfoClient) Info

func (WebSocketInfoClient) InfoJSON

func (c WebSocketInfoClient) InfoJSON() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL