$ pip install typeagentYou might also want to use a virtual environment or another tool like poetry or uv, as long as your tool can install wheels from PyPI.
STEVE We should really make a Python library for Structured RAG.
UMESH Who would be a good person to do the Python library?
GUIDO I volunteer to do the Python library. Give me a few months.from typeagent import create_conversation
from typeagent.transcripts.transcript import (
TranscriptMessage,
TranscriptMessageMeta,
)
def read_messages(filename) -> list[TranscriptMessage]:
messages: list[TranscriptMessage] = []
with open(filename, "r") as f:
for line in f:
# Parse each line into a TranscriptMessage
speaker, text_chunk = line.split(None, 1)
message = TranscriptMessage(
text_chunks=[text_chunk],
metadata=TranscriptMessageMeta(speaker=speaker),
)
messages.append(message)
return messages
async def main():
conversation = await create_conversation("demo.db", TranscriptMessage)
messages = read_messages("testdata.txt")
print(f"Indexing {len(messages)} messages...")
results = await conversation.add_messages_with_indexing(messages)
print(f"Indexed {results.messages_added} messages.")
print(f"Got {results.semrefs_added} semantic refs.")
if __name__ == "__main__":
import asyncio
asyncio.run(main())The minimal set of environment variables is:
export OPENAI_API_KEY=your-very-secret-openai-api-key
export OPENAI_MODEL=gpt-4oSome OpenAI setups will require some additional environment variables. See Environment Variables for more information. You will also find information there on how to use Azure-hosted OpenAI models.
$ python ingest.pyExpected output looks like:
0.027s -- Using OpenAI
Indexing 3 messages...
Indexed 3 messages.
Got 24 semantic refs.from typeagent import create_conversation
from typeagent.transcripts.transcript import TranscriptMessage
async def main():
conversation = await create_conversation("demo.db", TranscriptMessage)
question = "Who volunteered to do the python library?"
print("Q:", question)
answer = await conversation.query(question)
print("A:", answer)
if __name__ == "__main__":
import asyncio
asyncio.run(main())$ python query.pyExpected output looks like:
0.019s -- Using OpenAI
Q: Who volunteered to do the python library?
A: Guido volunteered to do the Python library.You can study the full documentation for create_conversation()
and conversation.query() in High-level API.
You can also study the source code at the typeagent-py repo.