Skip to main content

An API to manipulate strings semantically.

Project description

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.

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.match(query, targets)
print(f"Option at index {index} is the best: \"{response}\"")
Option at index 2 is the best: "The cat jumps over the fence"

Keep track of the costs with:

astr.log_history()
# or
print("Total Cost: ", astr.cumulative_cost)
History
----------------------
  Action: match
  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.summarize
  • astr.match
  • astr.split

coming soon:

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

More Examples

Split

from dotenv import load_dotenv

from aistrings import AiStrings

load_dotenv()

astr = AiStrings(provider_name="openai", model_name="gpt-4-0125-preview", temperature=1)

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)
astr.log_history()
print(response)
['I have never seen ', ' through a telescope, but I would really love to see it once.']

Join

from dotenv import load_dotenv

from aistrings import AiStrings

load_dotenv()

astr = AiStrings(provider_name="openai", model_name="gpt-4-0125-preview")

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

from dotenv import load_dotenv

from aistrings import AiStrings

load_dotenv()

astr = AiStrings(provider_name="openai", model_name="gpt-3.5-turbo-0125")
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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aistrings-0.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aistrings-0.1.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file aistrings-0.1.2.tar.gz.

File metadata

  • Download URL: aistrings-0.1.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.1.0

File hashes

Hashes for aistrings-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0a6daa30533a440a58dbd10da2b7d7c518faa086f788556f4bfb93e3ec6f165e
MD5 fcc6f6102b1f30afbbc2cd4744612fcf
BLAKE2b-256 68d35949e302f9cacd986e3d6fe7ef4e82797791db1156a5581f21bcdbfaaa51

See more details on using hashes here.

File details

Details for the file aistrings-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aistrings-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.1.0

File hashes

Hashes for aistrings-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b23d690285c1bbdf41887dd917202dbc073b16a346e46dd9640b82ed5993dfb
MD5 8d2346ee6bcc3dac48f266bc1d56b666
BLAKE2b-256 541279d775da93b4bc543e6e9bc703ecd11c4b44f53ecccdb5f1921c30c60c5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page