First thing you’ll need when you put your app in production is to store the conversation logs for your users or agents. To solve this we added ThreadsAPI to Tune Studio. See how to quickly start using it.

Threads & Messages

A thread is a conversation log between a user and the AI. For us a thread is a unit of thought for an AI. A thread is an ordered list of messages, where each message contains a role and content. Here’s how you can create a thread using our python package.

main.py
import tuneapi.types as tt
import tuneapi.apis as ta

# Create a thread
thread = tt.Thread(
    tt.system("You are an old pirate, you will respond to everything in tone of a pirate."),
    tt.human("What is the secret of the universe?"),
)

# use threads with any APIs
model = ta.TuneModel()
resp = model.chat(thread)
print(resp)

# > Arr matey, I ain't got the secret of the universe in me pockets, but I do know this ...
We support OpenAI format by default and tuneapi package manages the internal conversions for the provided API vendors like Anthropic

Storing Threads

You can store the threads in Tune Studio using the ThreadsAPI. To upload and save this thread use the following code.

main.py
# add the response to the thread and add title
thread.append(tt.assistant(resp))
thread.title = "my first thread"

# Store the thread
tapi = ThreadsAPI(
    tune_api_key = "your API key", # or set TUNE_API_KEY in env
    tune_org_id = "your org ID",   # or ser TUNE_ORG_ID in env
)

# put
tapi.put_thread(thread)
print(thread.id)

# > thread_4adcec65-ecde-423c-af30-64f55ef2e8ad

The API will add the unique ID for the thread and store it in the Studio.

Getting Threads

To get the thread back from the Studio, you can use the following code. Remember to pass messages = True to get the contents of the messages as well.

main.py
# Get the thread
thr_studio = tapi.get_thread(thread.id, messages = True)
print(len(thr_studio.chats))

# > 3