Skip to content

Commit

Permalink
adds early experimentation towards artifacts as assistant extension i…
Browse files Browse the repository at this point in the history
…n explorer assistant (#193)

WIP, disabled by default, first bits to convert prior approach to
assistant extension
  • Loading branch information
bkrabach authored Oct 31, 2024
1 parent aed4566 commit 335a245
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 20 deletions.
66 changes: 58 additions & 8 deletions assistants/explorer-assistant/assistant/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

import deepmerge
import openai_client
from assistant_extensions.artifacts import ArtifactsExtension
from assistant_extensions.artifacts._model import ArtifactsConfigModel
from assistant_extensions.attachments import AttachmentsExtension
from content_safety.evaluators import CombinedContentSafetyEvaluator
from openai.types.chat import ChatCompletionMessageParam
from semantic_workbench_api_model.workbench_model import (
AssistantStateEvent,
ConversationEvent,
ConversationMessage,
ConversationParticipant,
Expand All @@ -23,6 +26,7 @@
)
from semantic_workbench_assistant.assistant_app import (
AssistantApp,
AssistantContext,
BaseModelAssistantConfig,
ContentSafety,
ContentSafetyEvaluator,
Expand Down Expand Up @@ -67,6 +71,12 @@ async def content_evaluator_factory(context: ConversationContext) -> ContentSafe
content_interceptor=content_safety,
)


async def artifact_config_provider(context: AssistantContext) -> ArtifactsConfigModel:
return (await assistant_config.get(context)).extensions_config.artifacts


artifacts_extension = ArtifactsExtension(assistant, artifact_config_provider)
attachments_extension = AttachmentsExtension(assistant)

#
Expand Down Expand Up @@ -198,6 +208,10 @@ async def respond_to_conversation(
" your turn."
)

# add the artifact agent instruction prompt to the system message content
if config.extensions_config.artifacts.enabled:
system_message_content += f"\n\n{config.extensions_config.artifacts.instruction_prompt}"

# add the guardrails prompt to the system message content
system_message_content += f"\n\n{config.guardrails_prompt}"

Expand All @@ -210,7 +224,7 @@ async def respond_to_conversation(

# generate the attachment messages from the attachment agent
attachment_messages = await attachments_extension.get_completion_messages_for_attachments(
context, config=config.agents_config.attachment_agent
context, config=config.extensions_config.attachments
)

# add the attachment messages to the completion messages
Expand Down Expand Up @@ -302,13 +316,49 @@ async def respond_to_conversation(
# generate a response from the AI model
async with openai_client.create_client(config.service_config) as client:
try:
# call the OpenAI API to generate a completion
completion = await client.chat.completions.create(
messages=completion_messages,
model=config.request_config.openai_model,
max_tokens=config.request_config.response_tokens,
)
content = completion.choices[0].message.content
if config.extensions_config.artifacts.enabled:
response = await artifacts_extension.get_openai_completion_response(
client,
completion_messages,
config.request_config.openai_model,
config.request_config.response_tokens,
)

completion = response.completion
content = response.assistant_response
artifacts_to_create_or_update = response.artifacts_to_create_or_update

for artifact in artifacts_to_create_or_update:
artifacts_extension.create_or_update_artifact(
context,
artifact,
)
# send an event to notify the artifact state was updated
await context.send_conversation_state_event(
AssistantStateEvent(
state_id="artifacts",
event="updated",
state=None,
)
)
# send a focus event to notify the assistant to focus on the artifacts
await context.send_conversation_state_event(
AssistantStateEvent(
state_id="artifacts",
event="focus",
state=None,
)
)

else:
# call the OpenAI API to generate a completion
completion = await client.chat.completions.create(
messages=completion_messages,
model=config.request_config.openai_model,
max_tokens=config.request_config.response_tokens,
)

content = completion.choices[0].message.content

# get the total tokens used for the completion
completion_total_tokens = completion.usage.total_tokens if completion.usage else 0
Expand Down
27 changes: 18 additions & 9 deletions assistants/explorer-assistant/assistant/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Annotated

import openai_client
from assistant_extensions.artifacts import ArtifactsConfigModel
from assistant_extensions.attachments import AttachmentsConfigModel
from content_safety.evaluators import CombinedContentSafetyEvaluatorConfig
from pydantic import BaseModel, ConfigDict, Field
Expand All @@ -23,15 +24,23 @@
#


class AgentsConfigModel(BaseModel):
attachment_agent: Annotated[
class ExtensionsConfigModel(BaseModel):
attachments: Annotated[
AttachmentsConfigModel,
Field(
title="Attachment Agent Configuration",
description="Configuration for the attachment agent.",
title="Attachments Extension Configuration",
description="Configuration for the attachments extension.",
),
] = AttachmentsConfigModel()

artifacts: Annotated[
ArtifactsConfigModel,
Field(
title="Artifacts Extension Configuration",
description="Configuration for the artifacts extension.",
),
] = ArtifactsConfigModel()


class HighTokenUsageWarning(BaseModel):
enabled: Annotated[
Expand Down Expand Up @@ -190,13 +199,13 @@ class AssistantConfigModel(BaseModel):
UISchema(widget="radio"),
] = CombinedContentSafetyEvaluatorConfig()

agents_config: Annotated[
AgentsConfigModel,
extensions_config: Annotated[
ExtensionsConfigModel,
Field(
title="Agents Configuration",
description="Configuration for the assistant agents.",
title="Extensions Configuration",
description="Configuration for the assistant extensions.",
),
] = AgentsConfigModel()
] = ExtensionsConfigModel()

# add any additional configuration fields

Expand Down
5 changes: 4 additions & 1 deletion libraries/python/assistant-extensions/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
"**/__pycache__": true
},
"cSpell.words": [
"endregion",
"Excalidraw",
"openai",
"pdfplumber"
"pdfplumber",
"pydantic"
]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import attachments
from . import artifacts, attachments

__all__ = ["attachments"]
__all__ = ["artifacts", "attachments"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ._artifacts import ArtifactsExtension, ArtifactsProcessingErrorHandler
from ._model import Artifact, ArtifactsConfigModel

__all__ = ["ArtifactsExtension", "ArtifactsConfigModel", "Artifact", "ArtifactsProcessingErrorHandler"]
Loading

0 comments on commit 335a245

Please sign in to comment.