A Rust wrapper for the Text synthesization service TextSynth API

Overview

TextSynth

A (unofficial) Rust wrapper for the TextSynth API, a free (as of 01/21/22) text synthesization service.

Preparation

You must have an API key in order to use this library.

In order to get an API key, you must create an account at TextSynth.

After signing up and verifying your email address, you will be given an API key.

Basic Usage

The most commonly used types and traits are in the prelude. Import them first:

use textsynth::prelude::*;

We now need to create a TextSynth instance. This is where we can create engines, explained further down below. Pass the API key you got from the preparations to TextSynth::new.

"); let textsynth = TextSynth::new(api_key);">
let api_key = String::from("
    
     "
    );
let textsynth = TextSynth::new(api_key);

We now need to create an Engine. An Engine is where the fun stuff takes place. This is where you can complete text and log probabilities.

However, to create an engine, we need to provide a definition, an EngineDefinition. An EngineDefinition is information needed by the TextSynth API to determine which engine should be used. It also determines the maximum amount of tokens one can use in a text completion.

Usually, you can just use a EngineDefinition::GptJ6B to use the GPT-J model. There are other models available, but this one is probably the one you'll use the most.

let engine = textsynth.engine(EngineDefinition::GptJ6B);

Let's get started with the fun stuff.

Log Probabilities

This is logarithm of the probability that a continuation is generated after a context. It can be used to answer questions when only a few answers (such as yes/no) are possible. It can also be used to benchmark the models.

You need to provide two arguments into this function:

  • A context, the text you wish to predict a continuation for, and
  • A continuation, a non-empty string that you wish to predict against the context.
let context = String::from("The quick brown fox jumps over the lazy");
let continuation = String::from(" dog");
let continuation = NonEmptyString::new(continuation)?;
let log_probabilities = engine.log_probabilities(context, continuation).await??;

log_probabilities is of type LogProbabilities, which contains the result from the API. It contains multiple fields.

  • log_probability: This is a logarithm of the probability of generation of continuation preceded by the context. It is always <= 0.
  • is_greedy: true if continuation would be generated by greedy sampling from continuation.
  • total_tokens: Indicate the total number of tokens. It is useful to estimate the number of compute resourced used by the request.
println!("log probability = {}", log_probabilities.log_probability());
println!("is greedy = {}", log_probabilities.is_greedy());
println!("total tokens = {}", log_probabilities.total_tokens());

Text Completion

Since we'll only show basic usages here (for more advanced usages see the documentation), we'll only focus on the parts where most users will be interested in.

In order to begin the process of text completion, we need to provide a prompt, which is the input text.

let prompt = String::from("Once upon a time, there was")
let text_completion = engine.text_completion(prompt);

This creates a builder, which contains many method, but we'll only focus on the most common ones.

Immediate Text Completion

Well, "immediate" text completion. You still need to wait for the API to return the result.

This fetches the whole result, without splitting into multiple chunks.

let text_completion = text_completion.now().await??;

The text field contains the generated text itself.

println!("{}", text_completion.text());

Streaming Text Completion

This returns a stream of text completions.

let text_completion = text_completion.stream().await?;

Iterate over it and you can get the text.

// due to `async` limitations, we must iterate like this
while let Some(text_completion) = text_completion.next().await {
    let text_completion = text_completion???;
    print!("{}", text_completion.text());
    io::stdout().flush()?;
}

Examples

Examples can be found on the examples directory.

Application

An application which uses the library would be the synthtext program.

License

This is licensed under the MIT License.

You might also like...
A crate using DeepSpeech bindings to convert mic audio from speech to text

DS-TRANSCRIBER Need an Offline Speech To Text converter? Records your mic, and returns a String containing what was said. Features Begins transcriptio

Sorta Text Format in UTF-8

STFU-8: Sorta Text Format in UTF-8 STFU-8 is a hacky text encoding/decoding protocol for data that might be not quite UTF-8 but is still mostly UTF-8.

The fastest way to identify any mysterious text or analyze strings from a file, just ask `lemmeknow` !
The fastest way to identify any mysterious text or analyze strings from a file, just ask `lemmeknow` !

The fastest way to identify anything lemmeknow ⚡ Identify any mysterious text or analyze strings from a file, just ask lemmeknow. lemmeknow can be use

Source text parsing, lexing, and AST related functionality for Deno

Source text parsing, lexing, and AST related functionality for Deno.

better tools for text parsing

nom-text Goal: a library that extends nom to provide better tools for text formats (programming languages, configuration files). current needs Recogni

Font independent text analysis support for shaping and layout.

lipi Lipi (Sanskrit for 'writing, letters, alphabet') is a pure Rust crate that provides font independent text analysis support for shaping and layout

lingua-rs Python binding. An accurate natural language detection library, suitable for long and short text alike.

lingua-py lingua-rs Python binding. An accurate natural language detection library, suitable for long and short text alike. Installation pip install l

Makdown-like text parser.

Makdown-like text parser.

Ultra-fast, spookily accurate text summarizer that works on any language
Ultra-fast, spookily accurate text summarizer that works on any language

pithy 0.1.0 - an absurdly fast, strangely accurate, summariser Quick example: pithy -f your_file_here.txt --sentences 4 --help: Print this help messa

Owner
ALinuxPerson
I will hack the phantom codes.
ALinuxPerson
Text Expression Runner – Readable and easy to use text expressions

ter - Text Expression Runner ter is a cli to run text expressions and perform basic text operations such as filtering, ignoring and replacing on the c

Maximilian Schulke 72 Jul 31, 2022
An efficient and powerful Rust library for word wrapping text.

Textwrap Textwrap is a library for wrapping and indenting text. It is most often used by command-line programs to format dynamic output nicely so it l

Martin Geisler 322 Dec 26, 2022
👄 The most accurate natural language detection library in the Rust ecosystem, suitable for long and short text alike

Table of Contents What does this library do? Why does this library exist? Which languages are supported? How good is it? Why is it better than other l

Peter M. Stahl 569 Jan 3, 2023
A fast, low-resource Natural Language Processing and Text Correction library written in Rust.

nlprule A fast, low-resource Natural Language Processing and Error Correction library written in Rust. nlprule implements a rule- and lookup-based app

Benjamin Minixhofer 496 Jan 8, 2023
Rust wrapper for the BlingFire tokenization library

BlingFire in Rust blingfire is a thin Rust wrapper for the BlingFire tokenization library. Add the library to Cargo.toml to get started cargo add blin

Re:infer 14 Sep 5, 2022
Text calculator with support for units and conversion

cpc calculation + conversion cpc parses and evaluates strings of math, with support for units and conversion. 128-bit decimal floating points are used

Kasper 82 Jan 4, 2023
Find and replace text in source files

Ruplacer Find and replace text in source files: $ ruplacer old new src/ Patching src/a_dir/sub/foo.txt -- old is everywhere, old is old ++ new is ever

Tanker 331 Dec 28, 2022
bottom encodes UTF-8 text into a sequence comprised of bottom emoji

bottom encodes UTF-8 text into a sequence comprised of bottom emoji (with , sprinkled in for good measure) followed by ????. It can encode any valid UTF-8 - being a bottom transcends language, after all - and decode back into UTF-8.

Bottom Software Foundation 345 Dec 30, 2022
Semantic text segmentation. For sentence boundary detection, compound splitting and more.

NNSplit A tool to split text using a neural network. The main application is sentence boundary detection, but e. g. compound splitting for German is a

Benjamin Minixhofer 273 Dec 29, 2022
fastest text uwuifier in the west

uwuify fastest text uwuifier in the west transforms Hey... I think I really love you. Do you want a headpat? into hey... i think i w-weawwy wuv you.

Daniel Liu 1.2k Dec 29, 2022