### Install development tools Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Installs the necessary protoc plugins for generating gRPC and HTTP APIs. ```console go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \ github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \ google.golang.org/protobuf/cmd/protoc-gen-go \ google.golang.org/grpc/cmd/protoc-gen-go-grpc ``` -------------------------------- ### Install Cybertron library Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Use this command to fetch the Cybertron package into your Go project. ```console go get -u github.com/nlpodyssey/cybertron ``` -------------------------------- ### Run server instance Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Starts the Cybertron server on the specified address. ```console GOARCH=amd64 go run ./cmd/server -address 0.0.0.0:8080 ``` -------------------------------- ### Run machine translation example Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Executes the text generation example for English to Italian translation. ```console GOARCH=amd64 go run ./examples/textgeneration ``` -------------------------------- ### Configure and Start Cybertron Server Source: https://context7.com/nlpodyssey/cybertron/llms.txt Starts the Cybertron server using environment variables or command-line flags. Supports multiple task types including text generation and classification. ```bash # Create .env configuration file cat > .env << EOF CYBERTRON_MODEL=Helsinki-NLP/opus-mt-en-it CYBERTRON_MODELS_DIR=models CYBERTRON_MODEL_TASK=text-generation CYBERTRON_ADDRESS=0.0.0.0:8080 CYBERTRON_MODEL_DOWNLOAD=missing CYBERTRON_MODEL_CONVERSION=missing CYBERTRON_MODEL_CONVERSION_PRECISION=32 EOF # Start the server GOARCH=amd64 go run ./cmd/server # Or with command-line flags go run ./cmd/server \ -model "Helsinki-NLP/opus-mt-en-it" \ -models-dir "models" \ -task "text-generation" \ -address "0.0.0.0:8080" \ -model-download "missing" \ -model-conversion "missing" # Available task types: # - text-generation # - zero-shot-classification # - question-answering # - text-classification # - token-classification # - text-encoding # - language-modeling # TLS configuration (optional) # -tls true -tls-cert cert.pem -tls-key key.pem ``` -------------------------------- ### Run zero-shot classification example Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Executes the zero-shot classification example with specific labels. ```console GOARCH=amd64 go run ./examples/zeroshotclassification politics,business,science,technology,health,culture,sports ``` -------------------------------- ### Configure server environment Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Sets up the .env file required to run the server for machine translation tasks. ```console echo "CYBERTRON_MODEL=Helsinki-NLP/opus-mt-en-it" > .env echo "CYBERTRON_MODELS_DIR=models" >> .env echo "CYBERTRON_MODEL_TASK=text-generation" >> .env ``` -------------------------------- ### Connect to Cybertron via Go gRPC client Source: https://context7.com/nlpodyssey/cybertron/llms.txt Demonstrates initializing and using gRPC clients for text generation and question answering. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/client" "github.com/nlpodyssey/cybertron/pkg/tasks/textgeneration" "github.com/nlpodyssey/cybertron/pkg/tasks/questionanswering" ) func main() { // Client options opts := client.Options{ // Configure TLS, timeouts, etc. } // Text Generation client textGenClient := client.NewClientForTextGeneration("localhost:8080", opts) result, err := textGenClient.Generate( context.Background(), "Hello, how are you?", textgeneration.DefaultOptions(), ) if err != nil { log.Fatal(err) } fmt.Printf("Translation: %s\n", result.Texts[0]) // Question Answering client qaClient := client.NewClientForQuestionAnswering("localhost:8080", opts) qaResult, err := qaClient.ExtractAnswer( context.Background(), "Who founded Microsoft?", "Microsoft was founded by Bill Gates and Paul Allen in 1975.", &questionanswering.Options{MaxAnswers: 1}, ) if err != nil { log.Fatal(err) } fmt.Printf("Answer: %s (score: %.3f)\n", qaResult.Answers[0].Text, qaResult.Answers[0].Score) // Available client constructors: // - client.NewClientForTextGeneration(target, opts) // - client.NewClientForQuestionAnswering(target, opts) // - client.NewClientForZeroShotClassification(target, opts) // - client.NewClientForTextClassification(target, opts) // - client.NewClientForTokenClassification(target, opts) // - client.NewClientForTextEncoding(target, opts) // - client.NewClientForLanguageModeling(target, opts) } ``` -------------------------------- ### Load and Use a Pre-trained Model with tasks.Load Source: https://context7.com/nlpodyssey/cybertron/llms.txt Demonstrates the primary entry point for loading models from HuggingFace and performing text generation. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/textgeneration" ) func main() { // Configure the model loader config := &tasks.Config{ ModelsDir: "models", // Local directory for model storage ModelName: "Helsinki-NLP/opus-mt-en-it", // HuggingFace model identifier DownloadPolicy: tasks.DownloadMissing, // Download only if not present ConversionPolicy: tasks.ConvertMissing, // Convert only if not present ConversionPrecision: tasks.F32, // Use 32-bit floating point HubAccessToken: "", // Optional: for private models } // Load the model for text generation task model, err := tasks.Load[textgeneration.Interface](config) if err != nil { log.Fatal(err) } // Use the model result, err := model.Generate(context.Background(), "Hello world", textgeneration.DefaultOptions()) if err != nil { log.Fatal(err) } fmt.Println(result.Texts[0]) // Output: Ciao mondo } ``` -------------------------------- ### View server configuration help Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Displays all available flags and configuration options for the Cybertron server. ```console GOARCH=amd64 go run ./cmd/server -h ``` -------------------------------- ### Perform Text Generation Tasks Source: https://context7.com/nlpodyssey/cybertron/llms.txt Shows how to use default model constants for specific tasks like translation and configure generation options. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/textgeneration" ) func main() { // Machine Translation (English to Italian) translationModel := textgeneration.DefaultModelForMachineTranslation("en", "it") // Returns: "Helsinki-NLP/opus-mt-en-it" m, err := tasks.Load[textgeneration.Interface](&tasks.Config{ ModelsDir: "models", ModelName: translationModel, }) if err != nil { log.Fatal(err) } // Configure generation options opts := textgeneration.DefaultOptions() // opts.Temperature = nullable.Type[float64]{Value: 1.0, Valid: true} // opts.Sample = nullable.Type[bool]{Value: false, Valid: true} // opts.TopK = nullable.Type[int]{Value: 50, Valid: true} // opts.TopP = nullable.Type[float64]{Value: 0.9, Valid: true} result, err := m.Generate(context.Background(), "The quick brown fox jumps over the lazy dog.", opts) if err != nil { log.Fatal(err) } fmt.Printf("Translation: %s\n", result.Texts[0]) fmt.Printf("Score: %f\n", result.Scores[0]) // Available default models: // - textgeneration.DefaultModelForTextSummarization = "facebook/bart-large-cnn" // - textgeneration.DefaultModelForExtremeTextSummarization = "facebook/bart-large-xsum" // - textgeneration.DefaultModelForTextParaphrasing = "tuner007/pegasus_paraphrase" // - textgeneration.DefaultModelForAbstractiveQuestionAnswering = "vblagoje/bart_lfqa" // - textgeneration.DefaultModelForKeywordsGeneration = "bloomberg/KeyBART" } ``` -------------------------------- ### Server help output Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md The command line help output for the Cybertron server. ```console Usage of server: -address value server listening address -allowed-origins value allowed origins (comma separated) -loglevel value zerolog global level -model value model name (and sub-path of models-dir) -model-conversion value model conversion policy ("always"|"missing"|"never") -model-conversion-precision value floating-point bits of precision to use if the model is converted ("32"|"64") -model-download value model downloading policy ("always"|"missing"|"never") -models-dir value models's base directory -network value network type for server listening -task value type of inference/computation that the model can fulfill ("textgeneration"|"zero-shot-classification"|"question-answering"|"text-classification"|"token-classification"|"text-encoding") -tls value whether to enable TLS ("true"|"false") -tls-cert value TLS cert filename -tls-key value TLS key filename ``` -------------------------------- ### Test server with curl Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Sends a POST request to the running server to perform text generation. ```console curl -X 'POST' \ '0.0.0.0:8080/v1/generate' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "input": "You must be the change you wish to see in the world.", "parameters": {} }' ``` -------------------------------- ### Generate APIs Source: https://github.com/nlpodyssey/cybertron/blob/main/README.md Runs the generation command to update gRPC and HTTP API code. ```console go generate ./... ``` -------------------------------- ### Perform Zero-Shot Text Classification in Go Source: https://context7.com/nlpodyssey/cybertron/llms.txt Categorizes text into arbitrary labels without prior training. Use the MultiLabel parameter to allow multiple labels to be assigned to a single input. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/zeroshotclassifier" ) func main() { m, err := tasks.Load[zeroshotclassifier.Interface](&tasks.Config{ ModelsDir: "models", ModelName: zeroshotclassifier.DefaultModel, // "valhalla/distilbart-mnli-12-3" }) if err != nil { log.Fatal(err) } text := "The Federal Reserve announced a 0.25% interest rate hike today." params := zeroshotclassifier.Parameters{ CandidateLabels: []string{"politics", "business", "science", "technology", "health", "sports"}, HypothesisTemplate: zeroshotclassifier.DefaultHypothesisTemplate, // "This example is {}." MultiLabel: true, // Allow multiple labels to be true simultaneously } result, err := m.Classify(context.Background(), text, params) if err != nil { log.Fatal(err) } fmt.Println("Classification results:") for i, label := range result.Labels { fmt.Printf(" %s: %.3f\n", label, result.Scores[i]) } // Output: // Classification results: // business: 0.892 // politics: 0.456 // technology: 0.123 // ... } ``` -------------------------------- ### Execute Zero-Shot Classification via REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Categorizes input text into candidate labels without requiring model retraining. ```bash curl -X POST 'http://localhost:8080/v1/classify' \ -H 'Content-Type: application/json' \ -d '{ "input": "SpaceX successfully launched another batch of Starlink satellites.", "parameters": { "candidate_labels": ["technology", "politics", "sports", "science", "business"], "hypothesis_template": "This example is {}.", "multi_label": true } }' # Response: # { # "labels": ["technology", "science", "business", "politics", "sports"], # "scores": [0.89, 0.72, 0.34, 0.12, 0.05] # } ``` -------------------------------- ### Perform Abstractive Question Answering in Go Source: https://context7.com/nlpodyssey/cybertron/llms.txt Synthesizes answers from provided passages using a generative model. Requires the input to be formatted specifically for the model using the provided helper function. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/textgeneration" "github.com/nlpodyssey/cybertron/pkg/tasks/textgeneration/bart" ) func main() { m, err := tasks.Load[*bart.TextGeneration](&tasks.Config{ ModelsDir: "models", ModelName: textgeneration.DefaultModelForAbstractiveQuestionAnswering, }) if err != nil { log.Fatal(err) } question := "Why does water heated to room temperature feel colder than air?" passages := []string{ "when the skin is completely wet. The body continuously loses water by...", "Thermal contact conductance In physics, thermal contact conductance is the study of heat conduction between solid...", "are not in a relation of thermal equilibrium, heat will flow from the hotter to the colder...", } // Prepare input in the expected format for the model input := textgeneration.PrepareInputForAbstractiveQuestionAnswering(question, passages) // Format: "question: context:

passage1

passage2 ..." result, err := m.Generate(context.Background(), input, textgeneration.DefaultOptions()) if err != nil { log.Fatal(err) } fmt.Printf("Question: %s\n", question) fmt.Printf("Answer: %s\n", result.Texts[0]) } ``` -------------------------------- ### Perform Masked Language Modeling in Go Source: https://context7.com/nlpodyssey/cybertron/llms.txt Uses the default BERT model to predict masked tokens in a string. Requires the tasks package and a configured models directory. ```go package main import ( "context" "encoding/json" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/languagemodeling" ) func main() { m, err := tasks.Load[languagemodeling.Interface](&tasks.Config{ ModelsDir: "models", ModelName: languagemodeling.DefaultModel, // Default BERT model for MLM }) if err != nil { log.Fatal(err) } // Use [MASK] token to indicate positions to predict text := "The capital of France is [MASK]." params := languagemodeling.Parameters{ K: 10, // Return top 10 predictions for each mask } result, err := m.Predict(context.Background(), text, params) if err != nil { log.Fatal(err) } // Response contains predictions for each masked position jsonOutput, _ := json.MarshalIndent(result, "", " ") fmt.Println(string(jsonOutput)) // Output: // { // "tokens": [{ // "start": 25, // "end": 31, // "words": ["Paris", "Lyon", "Marseille", ...], // "scores": [0.92, 0.03, 0.01, ...] // }] // } ``` -------------------------------- ### Perform Supervised Text Classification Source: https://context7.com/nlpodyssey/cybertron/llms.txt Loads a classification model to categorize text and optionally filter results by threshold. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/textclassification" ) func main() { m, err := tasks.Load[textclassification.Interface](&tasks.Config{ ModelsDir: "models", ModelName: textclassification.DefaultModelForGeographicCategorizationMulti, // Predicts ISO 3166-1 alpha-3 country codes // Alternative: textclassification.DefaultModelForItalianNewsClassification }) if err != nil { log.Fatal(err) } text := "Tokyo Olympics ceremony draws record television audience across Asia" result, err := m.Classify(context.Background(), text) if err != nil { log.Fatal(err) } // Show top 5 predictions limit := 5 if len(result.Labels) < limit { limit = len(result.Labels) } fmt.Println("Geographic classification:") for i := 0; i < limit; i++ { fmt.Printf(" %s: %.3f\n", result.Labels[i], result.Scores[i]) } // Optional: Filter results by threshold filter := textclassification.Filter(0.1, 0.5) // keepThreshold, keepSumThreshold filtered := filter(result) fmt.Printf("Filtered labels: %v\n", filtered.Labels) } ``` -------------------------------- ### POST /v1/generate Source: https://context7.com/nlpodyssey/cybertron/llms.txt Performs text-to-text tasks such as translation or summarization based on the provided input and generation parameters. ```APIDOC ## POST /v1/generate ### Description Supports translation, summarization, and other text-to-text tasks via HTTP POST requests. ### Method POST ### Endpoint /v1/generate ### Request Body - **input** (string) - Required - The text to process. - **parameters** (object) - Optional - Generation settings like temperature, do_sample, top_k, and top_p. ### Request Example { "input": "You must be the change you wish to see in the world.", "parameters": { "temperature": 1.0, "do_sample": false, "top_k": 50, "top_p": 0.9 } } ### Response #### Success Response (200) - **texts** (array) - The generated text outputs. - **scores** (array) - Confidence scores for the generated texts. #### Response Example { "texts": ["Devi essere il cambiamento che desideri vedere nel mondo."], "scores": [0.9876] } ``` -------------------------------- ### Execute Text Generation via REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Sends a POST request to the generate endpoint for tasks like translation or summarization. ```bash # Text Generation / Translation curl -X POST 'http://localhost:8080/v1/generate' \ -H 'Content-Type: application/json' \ -d '{ "input": "You must be the change you wish to see in the world.", "parameters": { "temperature": 1.0, "do_sample": false, "top_k": 50, "top_p": 0.9 } }' # Response: # { # "texts": ["Devi essere il cambiamento che desideri vedere nel mondo."], # "scores": [0.9876] # } # Text Summarization (with appropriate model) curl -X POST 'http://localhost:8080/v1/generate' \ -H 'Content-Type: application/json' \ -d '{ "input": "Long article text to summarize...", "parameters": {} }' ``` -------------------------------- ### POST /v1/classify Source: https://context7.com/nlpodyssey/cybertron/llms.txt Categorizes text into provided candidate labels using zero-shot classification. ```APIDOC ## POST /v1/classify ### Description Categorizes text into provided candidate labels without requiring model retraining. ### Method POST ### Endpoint /v1/classify ### Request Body - **input** (string) - Required - The text to classify. - **parameters** (object) - Required - Contains candidate_labels, hypothesis_template, and multi_label boolean. ### Request Example { "input": "SpaceX successfully launched another batch of Starlink satellites.", "parameters": { "candidate_labels": ["technology", "politics", "sports", "science", "business"], "hypothesis_template": "This example is {}.", "multi_label": true } } ### Response #### Success Response (200) - **labels** (array) - The classified labels. - **scores** (array) - The confidence scores corresponding to the labels. #### Response Example { "labels": ["technology", "science", "business", "politics", "sports"], "scores": [0.89, 0.72, 0.34, 0.12, 0.05] } ``` -------------------------------- ### Perform token classification via HTTP REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Identifies and labels entities in text using Named Entity Recognition. ```bash curl -X POST 'http://localhost:8080/v1/classify' \ -H 'Content-Type: application/json' \ -d '{ "input": "Elon Musk announced that Tesla will open a new factory in Berlin, Germany.", "aggregation_strategy": 1 }' ``` -------------------------------- ### POST /v1/classify (Token Classification) Source: https://context7.com/nlpodyssey/cybertron/llms.txt Identifies and labels entities in text using Named Entity Recognition. ```APIDOC ## POST /v1/classify ### Description Named Entity Recognition endpoint identifies and labels entities in text. ### Method POST ### Endpoint /v1/classify ### Request Body - **input** (string) - Required - The text to analyze. - **aggregation_strategy** (integer) - Optional - 0 = NONE, 1 = SIMPLE. ### Request Example { "input": "Elon Musk announced that Tesla will open a new factory in Berlin, Germany.", "aggregation_strategy": 1 } ### Response #### Success Response (200) - **tokens** (array) - List of identified entities with text, start, end, label, and score. #### Response Example { "tokens": [ {"text": "Elon Musk", "start": 0, "end": 9, "label": "PER", "score": 0.998}, {"text": "Tesla", "start": 26, "end": 31, "label": "ORG", "score": 0.997}, {"text": "Berlin", "start": 58, "end": 64, "label": "LOC", "score": 0.995}, {"text": "Germany", "start": 66, "end": 73, "label": "LOC", "score": 0.996} ] } ``` -------------------------------- ### Generate Text Embeddings Source: https://context7.com/nlpodyssey/cybertron/llms.txt Encodes text into dense vectors using specified pooling strategies for semantic analysis. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/models/bert" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/textencoding" ) func main() { m, err := tasks.Load[textencoding.Interface](&tasks.Config{ ModelsDir: "models", ModelName: textencoding.DefaultModel, // "sentence-transformers/all-MiniLM-L6-v2" // Alternative: textencoding.DefaultModelMulti for 109 languages }) if err != nil { log.Fatal(err) } text := "Cybertron enables NLP in pure Go without Python dependencies." // Pooling strategies from bert package poolingStrategy := int(bert.MeanPooling) // Average of all token embeddings // Other options: // - bert.CLSTokenPooling: Use [CLS] token embedding // - bert.MaxPooling: Element-wise maximum result, err := m.Encode(context.Background(), text, poolingStrategy) if err != nil { log.Fatal(err) } // Get vector as float64 slice vector := result.Vector.Data().F64() fmt.Printf("Embedding dimension: %d\n", len(vector)) fmt.Printf("First 10 values: %v\n", vector[:10]) // Output: // Embedding dimension: 384 // First 10 values: [0.0234 -0.0891 0.0456 ...] // Use vectors for cosine similarity, clustering, etc. } ``` -------------------------------- ### POST /v1/encode Source: https://context7.com/nlpodyssey/cybertron/llms.txt Generates dense vector embeddings for semantic similarity and search applications. ```APIDOC ## POST /v1/encode ### Description Generate dense vector embeddings for semantic similarity and search applications. ### Method POST ### Endpoint /v1/encode ### Request Body - **input** (string) - Required - The text to encode. - **pooling_strategy** (integer) - Optional - 0 = CLS token, 1 = Mean pooling, 2 = Max pooling. ### Request Example { "input": "Machine learning enables computers to learn from data.", "pooling_strategy": 0 } ### Response #### Success Response (200) - **vector** (array) - The resulting dense vector embedding. #### Response Example { "vector": [0.0234, -0.0891, 0.0456, 0.1234] } ``` -------------------------------- ### Encode text via HTTP REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Generates dense vector embeddings for semantic similarity and search. ```bash curl -X POST 'http://localhost:8080/v1/encode' \ -H 'Content-Type: application/json' \ -d '{ "input": "Machine learning enables computers to learn from data.", "pooling_strategy": 0 }' ``` -------------------------------- ### Classify text via HTTP REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Performs supervised text classification using fine-tuned models. ```bash curl -X POST 'http://localhost:8080/v1/classify' \ -H 'Content-Type: application/json' \ -d '{ "input": "Major earthquake strikes central Japan causing widespread damage" }' ``` -------------------------------- ### POST /v1/classify (Text Classification) Source: https://context7.com/nlpodyssey/cybertron/llms.txt Performs supervised text classification using fine-tuned models with predefined label sets. ```APIDOC ## POST /v1/classify ### Description Performs supervised text classification using fine-tuned models with predefined label sets. ### Method POST ### Endpoint /v1/classify ### Request Body - **input** (string) - Required - The text to classify. ### Request Example { "input": "Major earthquake strikes central Japan causing widespread damage" } ### Response #### Success Response (200) - **labels** (array) - List of predicted labels. - **scores** (array) - Confidence scores for each label. #### Response Example { "labels": ["JPN", "ASI", "CHN", "KOR", "USA"], "scores": [0.94, 0.78, 0.12, 0.08, 0.03] } ``` -------------------------------- ### Execute Question Answering via REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Extracts answers from a passage based on a provided question. ```bash curl -X POST 'http://localhost:8080/v1/answer' \ -H 'Content-Type: application/json' \ -d '{ "question": "What is cloud computing?", "passage": "Cloud computing is a technology that allows individuals and businesses to access computing resources over the Internet. It enables users to utilize hardware and software managed by third parties at remote locations.", "options": { "max_answers": 3, "max_answers_len": 50, "max_candidates": 10, "min_score": 0.1 } }' # Response: # { # "answers": [ # { # "text": "a technology that allows individuals and businesses to access computing resources over the Internet", # "start": 20, # "end": 118, # "score": 0.8765 # } # ] # } ``` -------------------------------- ### Predict masked tokens via HTTP REST API Source: https://context7.com/nlpodyssey/cybertron/llms.txt Predicts masked tokens for text completion and language understanding. ```bash curl -X POST 'http://localhost:8080/v1/predict' \ -H 'Content-Type: application/json' \ -d '{ "input": "The [MASK] brown fox jumps over the lazy dog.", "parameters": { "k": 5 } }' ``` -------------------------------- ### Perform Token Classification (NER) Source: https://context7.com/nlpodyssey/cybertron/llms.txt Identifies entities like persons or locations in text using specific aggregation strategies. ```go package main import ( "context" "encoding/json" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/tokenclassification" ) func main() { m, err := tasks.Load[tokenclassification.Interface](&tasks.Config{ ModelsDir: "models", ModelName: tokenclassification.DefaultEnglishModel, // Supports: LOC, MISC, ORG, PER (CoNLL-2003) // Alternatives: // - tokenclassification.DefaultEnglishModelOntonotes (18 entity types) // - tokenclassification.DefaultModelMulti (9 languages) }) if err != nil { log.Fatal(err) } text := "Apple Inc. CEO Tim Cook announced new products at their headquarters in Cupertino, California." params := tokenclassification.Parameters{ AggregationStrategy: tokenclassification.AggregationStrategySimple, // Options: // - AggregationStrategyNone: classify each token individually // - AggregationStrategySimple: group tokens using IOB annotation } result, err := m.Classify(context.Background(), text, params) if err != nil { log.Fatal(err) } fmt.Println("Named Entities:") for _, token := range result.Tokens { fmt.Printf(" %s [%s] (%.3f) at [%d:%d]\n", token.Text, token.Label, token.Score, token.Start, token.End) } // Output: // Named Entities: // Apple Inc. [ORG] (0.998) at [0:10] // Tim Cook [PER] (0.997) at [16:24] // Cupertino [LOC] (0.995) at [71:80] // California [LOC] (0.994) at [82:92] // JSON output jsonOutput, _ := json.MarshalIndent(result, "", " ") fmt.Println(string(jsonOutput)) } ``` -------------------------------- ### Perform Extractive Question Answering in Go Source: https://context7.com/nlpodyssey/cybertron/llms.txt Extracts specific spans from a passage that answer a given question. Configure options like MaxAnswers and MinScore to filter the returned candidates. ```go package main import ( "context" "fmt" "log" "github.com/nlpodyssey/cybertron/pkg/tasks" "github.com/nlpodyssey/cybertron/pkg/tasks/questionanswering" ) func main() { // Load model for extractive QA m, err := tasks.Load[questionanswering.Interface](&tasks.Config{ ModelsDir: "models", ModelName: questionanswering.DefaultEnglishModel, // "deepset/bert-base-cased-squad2" // Alternative: questionanswering.DefaultItalianModel for Italian }) if err != nil { log.Fatal(err) } passage := `Cloud computing is a technology that allows individuals and businesses to access computing resources over the Internet. Major providers include Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP).` question := "What are the major cloud providers?" opts := &questionanswering.Options{ MaxAnswers: 3, // Return up to 3 answers MaxAnswerLength: 50, // Maximum answer length in tokens MinScore: 0.1, // Minimum confidence threshold MaxCandidates: 10, // Maximum candidates to consider } result, err := m.ExtractAnswer(context.Background(), question, passage, opts) if err != nil { log.Fatal(err) } for i, answer := range result.Answers { fmt.Printf("Answer %d: %s\n", i+1, answer.Text) fmt.Printf(" Score: %.4f\n", answer.Score) fmt.Printf(" Position: [%d:%d]\n", answer.Start, answer.End) } // Output: // Answer 1: Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP) // Score: 0.8542 // Position: [142:218] } ``` -------------------------------- ### POST /v1/answer Source: https://context7.com/nlpodyssey/cybertron/llms.txt Performs extractive question answering to find specific answers within a provided passage. ```APIDOC ## POST /v1/answer ### Description Finds answers within a given passage based on a specific question. ### Method POST ### Endpoint /v1/answer ### Request Body - **question** (string) - Required - The question to ask. - **passage** (string) - Required - The context text to search for the answer. - **options** (object) - Optional - Configuration for max_answers, max_answers_len, max_candidates, and min_score. ### Request Example { "question": "What is cloud computing?", "passage": "Cloud computing is a technology that allows individuals and businesses to access computing resources over the Internet.", "options": { "max_answers": 3, "max_answers_len": 50, "max_candidates": 10, "min_score": 0.1 } } ### Response #### Success Response (200) - **answers** (array) - List of identified answer objects containing text, start, end, and score. #### Response Example { "answers": [ { "text": "a technology that allows individuals and businesses to access computing resources over the Internet", "start": 20, "end": 118, "score": 0.8765 } ] } ``` -------------------------------- ### POST /v1/predict Source: https://context7.com/nlpodyssey/cybertron/llms.txt Predicts masked tokens for text completion and language understanding. ```APIDOC ## POST /v1/predict ### Description Predict masked tokens for text completion and language understanding. ### Method POST ### Endpoint /v1/predict ### Request Body - **input** (string) - Required - The text containing a [MASK] token. - **parameters** (object) - Optional - Configuration parameters like 'k' for top-k predictions. ### Request Example { "input": "The [MASK] brown fox jumps over the lazy dog.", "parameters": { "k": 5 } } ### Response #### Success Response (200) - **tokens** (array) - List of predicted words and their scores. #### Response Example { "tokens": [ { "start": 4, "end": 10, "words": ["quick", "little", "big", "old", "young"], "scores": [0.85, 0.05, 0.03, 0.02, 0.01] } ] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.