Merge branch 'zustand/io/migration' of github.com:logspace-ai/langflow into zustand/io/migration

This commit is contained in:
igorrCarvalho 2024-02-27 15:46:19 -03:00
commit 835a7f4e63
8 changed files with 125 additions and 149 deletions

View file

@ -1,5 +1,6 @@
import time import time
from typing import Optional from typing import Optional
import uuid
from fastapi import ( from fastapi import (
APIRouter, APIRouter,
@ -120,7 +121,8 @@ async def get_vertices(
# Now vertices is a list of lists # Now vertices is a list of lists
# We need to get the id of each vertex # We need to get the id of each vertex
# and return the same structure but only with the ids # and return the same structure but only with the ids
return VerticesOrderResponse(ids=vertices) run_id = uuid.uuid4()
return VerticesOrderResponse(ids=vertices, run_id=run_id)
except Exception as exc: except Exception as exc:
logger.error(f"Error checking build status: {exc}") logger.error(f"Error checking build status: {exc}")

View file

@ -225,6 +225,7 @@ class ApiKeyCreateRequest(BaseModel):
class VerticesOrderResponse(BaseModel): class VerticesOrderResponse(BaseModel):
ids: List[List[str]] ids: List[List[str]]
run_id: UUID
class ResultDataResponse(BaseModel): class ResultDataResponse(BaseModel):

View file

@ -1,36 +1,14 @@
from typing import Optional, Union from typing import Optional, Union
from langflow import CustomComponent
from langflow.field_typing import Text from langflow.field_typing import Text
from langflow.io.schema import ChatComponent
from langflow.schema import Record from langflow.schema import Record
class ChatInput(CustomComponent): class ChatInput(ChatComponent):
display_name = "Chat Input" display_name = "Chat Input"
description = "Used to get user input from the chat." description = "Used to get user input from the chat."
def build_config(self):
return {
"input_value": {
"input_types": ["Text"],
"display_name": "Message",
"multiline": True,
},
"sender": {
"options": ["Machine", "User"],
"display_name": "Sender Type",
},
"sender_name": {"display_name": "Sender Name"},
"session_id": {
"display_name": "Session ID",
"info": "Session ID of the chat history.",
},
"return_record": {
"display_name": "Return Record",
"info": "Return the message as a record containing the sender, sender_name, and session_id.",
},
}
def build( def build(
self, self,
sender: Optional[str] = "User", sender: Optional[str] = "User",
@ -39,22 +17,10 @@ class ChatInput(CustomComponent):
session_id: Optional[str] = None, session_id: Optional[str] = None,
return_record: Optional[bool] = False, return_record: Optional[bool] = False,
) -> Union[Text, Record]: ) -> Union[Text, Record]:
if return_record: return super().build(
if isinstance(input_value, Record): sender=sender,
# Update the data of the record sender_name=sender_name,
input_value.data["sender"] = sender input_value=input_value,
input_value.data["sender_name"] = sender_name session_id=session_id,
input_value.data["session_id"] = session_id return_record=return_record,
else: )
input_value = Record(
text=input_value,
data={
"sender": sender,
"sender_name": sender_name,
"session_id": session_id,
},
)
if not input_value:
input_value = ""
self.status = input_value
return input_value

View file

@ -9,55 +9,18 @@ class ChatOutput(CustomComponent):
display_name = "Chat Output" display_name = "Chat Output"
description = "Used to send a message to the chat." description = "Used to send a message to the chat."
field_config = {
"code": {
"show": True,
}
}
def build_config(self):
return {
"input_value": {"input_types": ["Text"], "display_name": "Message"},
"sender": {
"options": ["Machine", "User"],
"display_name": "Sender Type",
},
"sender_name": {"display_name": "Sender Name"},
"session_id": {
"display_name": "Session ID",
"info": "Session ID of the chat history.",
"input_types": ["Text"],
},
"return_record": {
"display_name": "Return Record",
"info": "Return the message as a record containing the sender, sender_name, and session_id.",
},
}
def build( def build(
self, self,
sender: Optional[str] = "Machine", sender: Optional[str] = "User",
sender_name: Optional[str] = "AI", sender_name: Optional[str] = "User",
session_id: Optional[str] = None,
input_value: Optional[str] = None, input_value: Optional[str] = None,
session_id: Optional[str] = None,
return_record: Optional[bool] = False, return_record: Optional[bool] = False,
) -> Union[Text, Record]: ) -> Union[Text, Record]:
if return_record: return super().build(
if isinstance(input_value, Record): sender=sender,
# Update the data of the record sender_name=sender_name,
input_value.data["sender"] = sender input_value=input_value,
input_value.data["sender_name"] = sender_name session_id=session_id,
input_value.data["session_id"] = session_id return_record=return_record,
else: )
input_value = Record(
text=input_value,
data={
"sender": sender,
"sender_name": sender_name,
"session_id": session_id,
},
)
if not input_value:
input_value = ""
self.status = input_value
return input_value

View file

View file

@ -0,0 +1,101 @@
import warnings
from typing import Optional, Union
from langflow import CustomComponent
from langflow.field_typing import Text
from langflow.memory import add_messages
from langflow.schema import Record
class ChatComponent(CustomComponent):
display_name = "Chat Component"
description = "Use as base for chat components."
def build_config(self):
return {
"input_value": {
"input_types": ["Text"],
"display_name": "Message",
"multiline": True,
},
"sender": {
"options": ["Machine", "User"],
"display_name": "Sender Type",
},
"sender_name": {"display_name": "Sender Name"},
"session_id": {
"display_name": "Session ID",
"info": "If provided, the message will be stored in the memory.",
},
"return_record": {
"display_name": "Return Record",
"info": "Return the message as a record containing the sender, sender_name, and session_id.",
},
}
def store_message(
self,
message: Union[Text, Record],
session_id: Optional[str] = None,
sender: Optional[str] = None,
sender_name: Optional[str] = None,
) -> list[Record]:
if not message:
warnings.warn("No message provided.")
return []
if not session_id or not sender or not sender_name:
raise ValueError(
"All of session_id, sender, and sender_name must be provided."
)
if not record:
record = []
if not session_id or not sender or not sender_name:
raise ValueError
for text in text:
record = Record(
text=text,
data={
"session_id": session_id,
"sender": sender,
"sender_name": sender_name,
},
)
record.append(record)
elif isinstance(record, Record):
record = [record]
self.status = record
record = add_messages(record)
return record
def build(
self,
sender: Optional[str] = "User",
sender_name: Optional[str] = "User",
input_value: Optional[str] = None,
session_id: Optional[str] = None,
return_record: Optional[bool] = False,
) -> Union[Text, Record]:
if return_record:
if isinstance(input_value, Record):
# Update the data of the record
input_value.data["sender"] = sender
input_value.data["sender_name"] = sender_name
input_value.data["session_id"] = session_id
else:
input_value = Record(
text=input_value,
data={
"sender": sender,
"sender_name": sender_name,
"session_id": session_id,
},
)
if not input_value:
input_value = ""
self.status = input_value
if session_id:
self.store_message(input_value, session_id, sender, sender_name)
return input_value

View file

@ -1,58 +0,0 @@
from typing import Any, Union
from langchain_core.prompts import PromptTemplate as LCPromptTemplate
from llama_index.prompts import PromptTemplate as LIPromptTemplate
PromptTemplateTypes = Union[LCPromptTemplate, LIPromptTemplate]
class GenericPromptTemplate:
def __init__(self, prompt_template: PromptTemplateTypes):
object.__setattr__(self, "prompt_template", prompt_template)
@property
def input_keys(self):
prompt_template = object.__getattribute__(self, "prompt_template")
if isinstance(prompt_template, LCPromptTemplate):
return prompt_template.input_variables
elif isinstance(prompt_template, LIPromptTemplate):
return prompt_template.template_vars
else:
raise TypeError(f"Unknown prompt template type {type(prompt_template)}")
def to_lc_prompt(self):
prompt_template = object.__getattribute__(self, "prompt_template")
if isinstance(prompt_template, LCPromptTemplate):
return prompt_template
elif isinstance(prompt_template, LIPromptTemplate):
return LCPromptTemplate.from_template(prompt_template.get_template())
else:
raise TypeError(f"Unknown prompt template type {type(prompt_template)}")
def to_li_prompt(self):
prompt_template = object.__getattribute__(self, "prompt_template")
if isinstance(prompt_template, LIPromptTemplate):
return prompt_template
elif isinstance(prompt_template, LCPromptTemplate):
return LIPromptTemplate(template=prompt_template.template)
else:
raise TypeError(f"Unknown prompt template type {type(prompt_template)}")
def __or__(self, other):
prompt_template = object.__getattribute__(self, "prompt_template")
if isinstance(prompt_template, LIPromptTemplate):
return self.to_lc_prompt() | other
else:
raise TypeError(f"Unknown prompt template type {type(other)}")
def __getattribute__(self, name: str) -> Any:
if name in {
"input_keys",
"to_lc_prompt",
"to_li_prompt",
"__or__",
"prompt_template",
}:
return object.__getattribute__(self, name)
prompt_template = object.__getattribute__(self, "prompt_template")
return getattr(prompt_template, name)

View file

@ -135,6 +135,7 @@ export type Component = {
export type VerticesOrderTypeAPI = { export type VerticesOrderTypeAPI = {
ids: Array<Array<string>>; ids: Array<Array<string>>;
run_id: string;
}; };
export type VertexBuildTypeAPI = { export type VertexBuildTypeAPI = {