Conversations in Select AI represent an interactive exchange between the user and the system, enabling users to query or interact with the database through a series of natural language prompts.
1. Conversation Object model¶
2. ConversationAttributes¶
- class select_ai.ConversationAttributes(title: str | None = 'New Conversation', description: str | None = None, retention_days: timedelta | None = datetime.timedelta(days=7), conversation_length: int | None = 10)¶
Conversation Attributes
- Parameters:
title (str) – Conversation Title
description (str) – Description of the conversation topic
retention_days (datetime.timedelta) – The number of days the conversation will be stored in the database from its creation date. If value is 0, the conversation will not be removed unless it is manually deleted by delete
conversation_length (int) – Number of prompts to store for this conversation
3. Conversation API¶
- class select_ai.Conversation(conversation_id: str | None = None, attributes: ConversationAttributes | None = None)¶
Conversation class can be used to create, update and delete conversations in the database
Typical usage is to combine this conversation object with an AI Profile.chat_session() to have context-aware conversations with the LLM provider
- Parameters:
conversation_id (str) – Conversation ID
attributes (ConversationAttributes) – Conversation attributes
- create() str¶
Creates a new conversation and returns the conversation_id to be used in context-aware conversations with LLMs
- Returns:
conversation_id
- delete(force: bool = False)¶
Drops the conversation
- classmethod fetch(conversation_id: str) Conversation¶
Fetch conversation attributes from the database and build a proxy object
- Parameters:
conversation_id (str) – Conversation ID
- get_attributes() ConversationAttributes¶
Get attributes of the conversation from the database
- classmethod list() Iterator[Conversation]¶
List all conversations
- Returns:
Iterator[VectorIndex]
- set_attributes(attributes: ConversationAttributes)¶
Updates the attributes of the conversation in the database
3.1. Create conversion¶
import os
import select_ai
user = os.getenv("SELECT_AI_USER")
password = os.getenv("SELECT_AI_PASSWORD")
dsn = os.getenv("SELECT_AI_DB_CONNECT_STRING")
select_ai.connect(user=user, password=password, dsn=dsn)
conversation_attributes = select_ai.ConversationAttributes(
title="History of Science",
description="LLM's understanding of history of science",
)
conversation = select_ai.Conversation(attributes=conversation_attributes)
conversation_id = conversation.create()
print("Created conversation with conversation id: ", conversation_id)
output:
Created conversation with conversation id: 3AB2ED3E-7E52-8000-E063-BE1A000A15B6
3.2. Chat session¶
import os
import select_ai
user = os.getenv("SELECT_AI_USER")
password = os.getenv("SELECT_AI_PASSWORD")
dsn = os.getenv("SELECT_AI_DB_CONNECT_STRING")
select_ai.connect(user=user, password=password, dsn=dsn)
profile = select_ai.Profile(profile_name="oci_ai_profile")
conversation_attributes = select_ai.ConversationAttributes(
title="History of Science",
description="LLM's understanding of history of science",
)
conversation = select_ai.Conversation(attributes=conversation_attributes)
with profile.chat_session(conversation=conversation, delete=True) as session:
print(
"Conversation ID for this session is:",
conversation.conversation_id,
)
response = session.chat(
prompt="What is importance of history of science ?"
)
print(response)
response = session.chat(
prompt="Elaborate more on 'Learning from past mistakes'"
)
print(response)
output:
Conversation ID for this session is: 380A1910-5BF2-F7A1-E063-D81A000A3FDA
The importance of the history of science lies in its ability to provide a comprehensive understanding of the development of scientific knowledge and its impact on society. Here are some key reasons why the history of science is important:
1. **Contextualizing Scientific Discoveries**: The history of science helps us understand the context in which scientific discoveries were made, including the social, cultural, and intellectual climate of the time. This context is essential for appreciating the significance and relevance of scientific findings.
..
..
The history of science is replete with examples of mistakes, errors, and misconceptions that have occurred over time. By studying these mistakes, scientists and researchers can gain valuable insights into the pitfalls and challenges that have shaped the development of scientific knowledge. Learning from past mistakes is essential for several reasons:
...
...
3.3. List conversations¶
import os
import select_ai
user = os.getenv("SELECT_AI_USER")
password = os.getenv("SELECT_AI_PASSWORD")
dsn = os.getenv("SELECT_AI_DB_CONNECT_STRING")
select_ai.connect(user=user, password=password, dsn=dsn)
for conversation in select_ai.Conversation().list():
print(conversation.conversation_id)
print(conversation.attributes)
output:
5275A80-A290-DA17-E063-151B000AD3B4
ConversationAttributes(title='History of Science', description="LLM's understanding of history of science", retention_days=7)
37DF777F-F3DA-F084-E063-D81A000A53BE
ConversationAttributes(title='History of Science', description="LLM's understanding of history of science", retention_days=7)
3.4. Delete conversation¶
import os
import select_ai
user = os.getenv("SELECT_AI_USER")
password = os.getenv("SELECT_AI_PASSWORD")
dsn = os.getenv("SELECT_AI_DB_CONNECT_STRING")
select_ai.connect(user=user, password=password, dsn=dsn)
conversation = select_ai.Conversation(
conversation_id="37DDC22E-11C8-3D49-E063-D81A000A85FE"
)
conversation.delete(force=True)
print(
"Deleted conversation with conversation id: ",
conversation.conversation_id,
)
output:
Deleted conversation with conversation id: 37DDC22E-11C8-3D49-E063-D81A000A85FE
4. AsyncConversation API¶
- class select_ai.AsyncConversation(conversation_id: str | None = None, attributes: ConversationAttributes | None = None)¶
AsyncConversation class can be used to create, update and delete conversations in the database in an async manner
Typical usage is to combine this conversation object with an AsyncProfile.chat_session() to have context-aware conversations
- Parameters:
conversation_id (str) – Conversation ID
attributes (ConversationAttributes) – Conversation attributes
- async create() str¶
Creates a new conversation and returns the conversation_id to be used in context-aware conversations with LLMs
- Returns:
conversation_id
- async delete(force: bool = False)¶
Delete the conversation
- async classmethod fetch(conversation_id: str) AsyncConversation¶
Fetch conversation attributes from the database
- async get_attributes() ConversationAttributes¶
Get attributes of the conversation from the database
- classmethod list() AsyncGenerator[AsyncConversation, None]¶
List all conversations
- Returns:
Iterator[VectorIndex]
- async set_attributes(attributes: ConversationAttributes)¶
Updates the attributes of the conversation
4.1. Async chat session¶
import asyncio
import os
import select_ai
user = os.getenv("SELECT_AI_USER")
password = os.getenv("SELECT_AI_PASSWORD")
dsn = os.getenv("SELECT_AI_DB_CONNECT_STRING")
async def main():
await select_ai.async_connect(user=user, password=password, dsn=dsn)
async_profile = await select_ai.AsyncProfile(
profile_name="async_oci_ai_profile"
)
conversation_attributes = select_ai.ConversationAttributes(
title="History of Science",
description="LLM's understanding of history of science",
)
async_conversation = select_ai.AsyncConversation(
attributes=conversation_attributes
)
async with async_profile.chat_session(
conversation=async_conversation, delete=True
) as async_session:
response = await async_session.chat(
prompt="What is importance of history of science ?"
)
print(response)
response = await async_session.chat(
prompt="Elaborate more on 'Learning from past mistakes'"
)
print(response)
asyncio.run(main())
output:
Conversation ID for this session is: 380A1910-5BF2-F7A1-E063-D81A000A3FDA
The importance of the history of science lies in its ability to provide a comprehensive understanding of the development of scientific knowledge and its impact on society. Here are some key reasons why the history of science is important:
1. **Contextualizing Scientific Discoveries**: The history of science helps us understand the context in which scientific discoveries were made, including the social, cultural, and intellectual climate of the time. This context is essential for appreciating the significance and relevance of scientific findings.
..
..
The history of science is replete with examples of mistakes, errors, and misconceptions that have occurred over time. By studying these mistakes, scientists and researchers can gain valuable insights into the pitfalls and challenges that have shaped the development of scientific knowledge. Learning from past mistakes is essential for several reasons:
...
...
4.2. Async list conversations¶
import asyncio
import os
import select_ai
user = os.getenv("SELECT_AI_USER")
password = os.getenv("SELECT_AI_PASSWORD")
dsn = os.getenv("SELECT_AI_DB_CONNECT_STRING")
async def main():
await select_ai.async_connect(user=user, password=password, dsn=dsn)
async for conversation in select_ai.AsyncConversation().list():
print(conversation.conversation_id)
print(conversation.attributes)
asyncio.run(main())
output:
5275A80-A290-DA17-E063-151B000AD3B4
ConversationAttributes(title='History of Science', description="LLM's understanding of history of science", retention_days=7)
37DF777F-F3DA-F084-E063-D81A000A53BE
ConversationAttributes(title='History of Science', description="LLM's understanding of history of science", retention_days=7)