Skip to content

Commit

Permalink
docs(lab-3135): add docstring for create_chat_item llm method
Browse files Browse the repository at this point in the history
  • Loading branch information
paulruelle committed Sep 25, 2024
1 parent d317e31 commit 504d330
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/kili/adapters/kili_api_gateway/llm/operations_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get_update_project_model_mutation,
)
from kili.domain.llm import (
ChatItemDict,
ModelDict,
ModelToCreateInput,
ModelToUpdateInput,
Expand Down Expand Up @@ -183,12 +184,12 @@ def create_llm_asset(
result = self.graphql_client.execute(mutation, variables)
return result["createLLMAsset"]

def create_chat_item(self, asset_id: str, label_id: str, prompt: str) -> Dict:
def create_chat_item(self, asset_id: str, label_id: str, prompt: str) -> List[ChatItemDict]:
"""Create a chat item associated with an asset."""
data = map_create_chat_item_input(label_id, prompt)
where = map_asset_where(asset_id)
variables = {"data": data, "where": where}
fragment = fragment_builder(["content", "id", "labelId", "modelId", "parentId", "role"])
mutation = get_create_chat_item_mutation(fragment)
result = self.graphql_client.execute(mutation, variables)
return result["createChatItem"]
return [cast(ChatItemDict, item) for item in result["createChatItem"]]
11 changes: 11 additions & 0 deletions src/kili/domain/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ class ProjectModelDict(TypedDict):
id: str
configuration: Dict[str, Any]
model: ModelDict


class ChatItemDict(TypedDict):
"""Dict that represents a ChatItem."""

content: str
id: str
labelId: str
modelId: str
parentId: str
role: ChatItemRole
27 changes: 26 additions & 1 deletion src/kili/llm/presentation/client/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from kili.domain.asset import AssetExternalId, AssetFilters, AssetId
from kili.domain.llm import (
AzureOpenAICredentials,
ChatItemDict,
ModelDict,
ModelToCreateInput,
ModelToUpdateInput,
Expand Down Expand Up @@ -346,7 +347,31 @@ def delete_project_model(self, project_model_id: str) -> bool:
"""
return self.kili_api_gateway.delete_project_model(project_model_id)

def create_conversation(self, project_id: str, prompt: str):
def create_conversation(self, project_id: str, prompt: str) -> List[ChatItemDict]:
# pylint: disable=line-too-long
"""Create a new conversation in an LLM project starting with a user's prompt.
This method initiates a new conversation in the specified project by:
- Creating an LLM asset and label associated with the current user.
- Adding the user's prompt as the first chat item.
- Automatically generating assistant responses using the project's models.
Args:
project_id: The identifier of the project where the conversation will be created.
prompt: The initial prompt or message from the user to start the conversation.
Returns:
A list of chat items in the conversation, including the user's prompt and the assistant's responses.
Examples:
>>> PROMPT = "Hello, how can I improve my coding skills?"
>>> chat_items = kili.llm.create_conversation(project_id="your_project_id", prompt=PROMPT)
Notes:
- The first chat item corresponds to the user's prompt.
- The subsequent chat items are assistant responses generated by the project's models.
- An LLM asset and a label are created in the project with status "TODO" and labelType "PREDICTION".
"""
user_id = self.kili_api_gateway.get_current_user(["id"])["id"]
llm_asset = self.kili_api_gateway.create_llm_asset(
project_id=project_id,
Expand Down

0 comments on commit 504d330

Please sign in to comment.