Skip to main content

AI Strings

AiStrings uses language models to build a string API that is aware of semantics. The built in string APIs operate on syntax or morphology. For example matching two strings with a regex or a fuzzy string matching algorithm will never match 2 strings that essentially mean the same but look completely different.

AiStrings provides semantics aware string APIs with a familiar interface.

pip install aistrings

Make sure to set OPENAI_API_KEY in your .env file or directly in your environment.

from dotenv import load_dotenv
from aistrings import AiStrings

load_dotenv()

astr = AiStrings(provider_name="openai", model_name="gpt-3.5-turbo-0125")

targets = [
    "The cat sleeps too much",
    "The dog jumps over the fence",
    "The cat jumps over the fence",
    "I am so happy"
]
query = "The cat is very agile!"

response, index = astr.find(query, targets)
print(f"Index: {index}, Target: \"{response}\"")
Index: 2, Target: "The cat jumps over the fence"

Keep track of the costs with:

astr.log_history()
# or
print("Total Cost: ", astr.cumulative_cost)
History
----------------------
  Action: find
  Input: The cat is very agile!
  Output: The cat jumps over the fence
  Cost: 4.35e-05
  Time: 2024-05-14 14:50:25.698692

Total Cost: 4.35e-05
----------------------
# or
Total Cost: 4.35e-05

Currently available operations:

  • astr.find
  • astr.split
  • astr.replace
  • astr.substr
  • astr.match
  • astr.summarize

coming soon:

  • astr.translate
  • astr.answer
  • astr.is_factual
  • astr.make_verbose
  • astr.elaborate
  • astr.correct_grammar
  • astr.detect_lang
  • astr.detect_sentiment

More Examples

For all examples first run

from dotenv import load_dotenv
from aistrings import AiStrings
load_dotenv()
astr = AiStrings(provider_name="openai", model_name="gpt-4-0125-preview")

Replace

text = "I have never seen Saturn through a telescope, but I would really love to see it once."
criterion = "Replace every single verb in the text with the word cat."
response = astr.replace(text, criterion)
print(response)
I cat never cat Saturn through a telescope, but I cat really cat to cat it once.

Split

text = "I have never seen Saturn through a telescope, but I would really love to see it once."
criterion = "Split the text using names of planets of the solar system as separators."
response = astr.split(text, criterion)
print(response)
['I have never seen ', ' through a telescope, but I would really love to see it once.']

Join

text_list = [
    "The cat sleeps too much during the day, so it wakes up in the night and wants to play.",
    "When the cat wakes up in the night and wants to play it usually starts walking across my pillow.",
]
criterion = "Join the texts and remove duplicate information and use as few words as possible."
response = astr.join(text_list, criterion)
print(response)
The cat sleeps too much during the day, so it wakes up in the night and wants to play, usually starting by walking across my pillow.

Summarize

response = astr.summarize(
    "Mars is the fourth planet from the Sun. The surface of Mars is orange-red because it is covered in iron(III) oxide dust, giving it the nickname 'the Red Planet'.[21][22]"
    " Mars is among the brightest objects in Earth's sky, and its high-contrast albedo features have made it a common subject for telescope viewing. "
    "It is classified as a terrestrial planet and is the second smallest of the Solar System's planets with a diameter of 6,779 km (4,212 mi). "
    "In terms of orbital motion, a Martian solar day (sol) is equal to 24.5 hours, and a Martian solar year is equal to 1.88 Earth years (687 Earth days). "
    "Mars has two natural satellites that are small and irregular in shape: Phobos and Deimos. "
)
print(response)

Output:

Mars is the fourth planet from the Sun, known as the Red Planet due to its orange-red surface covered in iron(III) oxide dust. 
It is a terrestrial planet with a diameter of 6,779 km and has a solar day of 24.5 hours and a solar year of 1.88 Earth years. 
Mars has two small, irregular-shaped natural satellites: Phobos and Deimos.

Metadata

Release files for aistrings 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aistrings 0.1.3
File Size Uploaded Provenance Actions
aistrings-0.1.3.tar.gz 5.5 kB Without provenance Details for aistrings-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aistrings 0.1.3
File Interpreter ABI Platform Actions
aistrings-0.1.3-py3-none-any.whl Python 3 none any Details for aistrings-0.1.3-py3-none-any.whl

Total release size: 12.3 kB

Release files / aistrings-0.1.3.tar.gz

Download URL aistrings-0.1.3.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
97bdca0cea64793f120b162aa4b10fff741f59770762b74f0ae04392ccadd4fd
BLAKE2b-256 checksum
How to use checksums
5c83c43c242871a8d6378f58c9f24cbf9ac330101c3782b75c724dfd5de951c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.1.0

Release files / aistrings-0.1.3-py3-none-any.whl

Download URL aistrings-0.1.3-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e8f2e3a5c6cf1ada3e84caaa965651d045fb0b5e6d5ff48ada2844009577765
BLAKE2b-256 checksum
How to use checksums
13fb085c2290050ffe7fc414e7191d81c2b24c536c87299e0b035d7831295a6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.1.0

Security for aistrings 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page