fix: update assistants components and add integrations tests (#3887)

* fixes and integrations tests

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Sebastián Estévez 2024-09-24 08:27:15 -04:00 committed by GitHub
commit 0f97d359f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 87 additions and 8 deletions

View file

@ -1,4 +1,6 @@
from .create_assistant import AssistantsCreateAssistant
from .create_thread import AssistantsCreateThread
from .dotenv import Dotenv
from .get_assistant import AssistantsGetAssistantName
from .list_assistants import AssistantsListAssistants
from .run import AssistantsRun
@ -8,6 +10,8 @@ __all__ = [
"AssistantsCreateAssistant",
"AssistantsGetAssistantName",
"AssistantsListAssistants",
"AssistantsCreateThread",
"AssistantsRun",
"GetEnvVar",
"Dotenv",
]

View file

@ -10,6 +10,7 @@ class AssistantsCreateAssistant(Component):
icon = "bot"
display_name = "Create Assistant"
description = "Creates an Assistant and returns it's id"
client = patch(OpenAI())
inputs = [
StrInput(
@ -45,8 +46,7 @@ class AssistantsCreateAssistant(Component):
def process_inputs(self) -> Message:
print(f"env_set is {self.env_set}")
client = patch(OpenAI())
assistant = client.beta.assistants.create(
assistant = self.client.beta.assistants.create(
name=self.assistant_name,
instructions=self.instructions,
model=self.model,

View file

@ -9,6 +9,7 @@ from langflow.template import Output
class AssistantsCreateThread(Component):
display_name = "Create Assistant Thread"
description = "Creates a thread and returns the thread id"
client = patch(OpenAI())
inputs = [
MultilineInput(
@ -23,9 +24,7 @@ class AssistantsCreateThread(Component):
]
def process_inputs(self) -> Message:
client = patch(OpenAI())
thread = client.beta.threads.create()
thread = self.client.beta.threads.create()
thread_id = thread.id
message = Message(text=thread_id)

View file

@ -10,6 +10,7 @@ from langflow.template import Output
class AssistantsGetAssistantName(Component):
display_name = "Get Assistant name"
description = "Assistant by id"
client = patch(OpenAI())
inputs = [
StrInput(
@ -29,7 +30,6 @@ class AssistantsGetAssistantName(Component):
]
def process_inputs(self) -> Message:
patch(OpenAI())
assistant = self.client.beta.assistants.retrieve(
assistant_id=self.assistant_id,
)

View file

@ -9,14 +9,14 @@ from langflow.template.field.base import Output
class AssistantsListAssistants(Component):
display_name = "List Assistants"
description = "Returns a list of assistant id's"
client = patch(OpenAI())
outputs = [
Output(display_name="Assistants", name="assistants", method="process_inputs"),
]
def process_inputs(self) -> Message:
patch(OpenAI())
assistants = self.client.beta.assistants.list()
assistants = self.client.beta.assistants.list().data
id_list = [assistant.id for assistant in assistants]
message = Message(
# get text from list

View file

@ -13,6 +13,7 @@ from langflow.template import Output
class AssistantsRun(Component):
display_name = "Run Assistant"
description = "Executes an Assistant Run against a thread"
client = patch(OpenAI())
def update_build_config(
self,

View file

@ -56,6 +56,9 @@ class Template(BaseModel):
_input = Input(**value)
data["fields"].append(_input)
# Handles components with no inputs
if "fields" not in data:
data["fields"] = []
return cls(**data)
# For backwards compatibility

View file

@ -0,0 +1,72 @@
import pytest
from tests.integration.utils import run_single_component
async def test_list_assistants():
from langflow.components.astra_assistants import AssistantsListAssistants
results = await run_single_component(
AssistantsListAssistants,
inputs={},
)
assert results["assistants"].text is not None
@pytest.mark.api_key_required
@pytest.mark.asyncio
async def test_create_assistants():
from langflow.components.astra_assistants import AssistantsCreateAssistant
results = await run_single_component(
AssistantsCreateAssistant,
inputs={
"assistant_name": "artist-bot",
"instructions": "reply only with ascii art",
"model": "gpt-4o-mini",
},
)
assistant_id = results["assistant_id"].text
assert assistant_id is not None
await test_list_assistants()
await get_assistant_name(assistant_id)
thread_id = await test_create_thread()
await run_assistant(assistant_id, thread_id)
async def test_create_thread():
from langflow.components.astra_assistants import AssistantsCreateThread
results = await run_single_component(
AssistantsCreateThread,
inputs={},
)
thread_id = results["thread_id"].text
assert thread_id is not None
return thread_id
async def get_assistant_name(assistant_id):
from langflow.components.astra_assistants import AssistantsGetAssistantName
results = await run_single_component(
AssistantsGetAssistantName,
inputs={
"assistant_id": assistant_id,
},
)
assert results["assistant_name"].text is not None
async def run_assistant(assistant_id, thread_id):
from langflow.components.astra_assistants import AssistantsRun
results = await run_single_component(
AssistantsRun,
inputs={
"assistant_id": assistant_id,
"user_message": "hello",
"thread_id": thread_id,
},
)
assert results["assistant_response"].text is not None