Merge remote-tracking branch 'origin/main' into dev
This commit is contained in:
commit
397ff12760
3 changed files with 38 additions and 47 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "langflow"
|
name = "langflow"
|
||||||
version = "0.0.69"
|
version = "0.0.71"
|
||||||
description = "A Python package with a built-in web application"
|
description = "A Python package with a built-in web application"
|
||||||
authors = ["Logspace <contact@logspace.ai>"]
|
authors = ["Logspace <contact@logspace.ai>"]
|
||||||
maintainers = [
|
maintainers = [
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ def instantiate_based_on_type(class_object, base_type, node_type, params):
|
||||||
if base_type == "agents":
|
if base_type == "agents":
|
||||||
return instantiate_agent(class_object, params)
|
return instantiate_agent(class_object, params)
|
||||||
elif base_type == "prompts":
|
elif base_type == "prompts":
|
||||||
return instantiate_prompt(node_type, params)
|
return instantiate_prompt(class_object, node_type, params)
|
||||||
elif base_type == "tools":
|
elif base_type == "tools":
|
||||||
return instantiate_tool(node_type, class_object, params)
|
return instantiate_tool(node_type, class_object, params)
|
||||||
elif base_type == "toolkits":
|
elif base_type == "toolkits":
|
||||||
|
|
@ -77,12 +77,12 @@ def instantiate_agent(class_object, params):
|
||||||
return load_agent_executor(class_object, params)
|
return load_agent_executor(class_object, params)
|
||||||
|
|
||||||
|
|
||||||
def instantiate_prompt(node_type, params):
|
def instantiate_prompt(class_object, node_type, params):
|
||||||
if node_type == "ZeroShotPrompt":
|
if node_type == "ZeroShotPrompt":
|
||||||
if "tools" not in params:
|
if "tools" not in params:
|
||||||
params["tools"] = []
|
params["tools"] = []
|
||||||
return ZeroShotAgent.create_prompt(**params)
|
return ZeroShotAgent.create_prompt(**params)
|
||||||
return None # Or some other default action
|
return class_object(**params)
|
||||||
|
|
||||||
|
|
||||||
def instantiate_tool(node_type, class_object, params):
|
def instantiate_tool(node_type, class_object, params):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
import io
|
import io
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict, List, Tuple
|
||||||
|
|
||||||
from chromadb.errors import NotEnoughElementsException # type: ignore
|
from chromadb.errors import NotEnoughElementsException # type: ignore
|
||||||
|
|
||||||
|
|
@ -8,6 +8,7 @@ from langflow.api.callback import AsyncStreamingLLMCallbackHandler, StreamingLLM
|
||||||
from langflow.cache.base import compute_dict_hash, load_cache, memoize_dict
|
from langflow.cache.base import compute_dict_hash, load_cache, memoize_dict
|
||||||
from langflow.graph.graph import Graph
|
from langflow.graph.graph import Graph
|
||||||
from langflow.utils.logger import logger
|
from langflow.utils.logger import logger
|
||||||
|
from langchain.schema import AgentAction
|
||||||
|
|
||||||
|
|
||||||
def load_langchain_object(data_graph, is_first_message=False):
|
def load_langchain_object(data_graph, is_first_message=False):
|
||||||
|
|
@ -175,33 +176,25 @@ async def get_result_and_steps(langchain_object, message: str, **kwargs):
|
||||||
langchain_object.return_intermediate_steps = True
|
langchain_object.return_intermediate_steps = True
|
||||||
|
|
||||||
fix_memory_inputs(langchain_object)
|
fix_memory_inputs(langchain_object)
|
||||||
|
try:
|
||||||
|
async_callbacks = [AsyncStreamingLLMCallbackHandler(**kwargs)]
|
||||||
|
output = await langchain_object.acall(chat_input, callbacks=async_callbacks)
|
||||||
|
except Exception as exc:
|
||||||
|
# make the error message more informative
|
||||||
|
logger.debug(f"Error: {str(exc)}")
|
||||||
|
sync_callbacks = [StreamingLLMCallbackHandler(**kwargs)]
|
||||||
|
output = langchain_object(chat_input, callbacks=sync_callbacks)
|
||||||
|
|
||||||
with io.StringIO() as output_buffer, contextlib.redirect_stdout(output_buffer):
|
intermediate_steps = (
|
||||||
try:
|
output.get("intermediate_steps", []) if isinstance(output, dict) else []
|
||||||
async_callbacks = [AsyncStreamingLLMCallbackHandler(**kwargs)]
|
)
|
||||||
output = await langchain_object.acall(
|
|
||||||
chat_input, callbacks=async_callbacks
|
|
||||||
)
|
|
||||||
except Exception as exc:
|
|
||||||
# make the error message more informative
|
|
||||||
logger.debug(f"Error: {str(exc)}")
|
|
||||||
sync_callbacks = [StreamingLLMCallbackHandler(**kwargs)]
|
|
||||||
output = langchain_object(chat_input, callbacks=sync_callbacks)
|
|
||||||
|
|
||||||
intermediate_steps = (
|
|
||||||
output.get("intermediate_steps", []) if isinstance(output, dict) else []
|
|
||||||
)
|
|
||||||
|
|
||||||
result = (
|
|
||||||
output.get(langchain_object.output_keys[0])
|
|
||||||
if isinstance(output, dict)
|
|
||||||
else output
|
|
||||||
)
|
|
||||||
if intermediate_steps:
|
|
||||||
thought = format_intermediate_steps(intermediate_steps)
|
|
||||||
else:
|
|
||||||
thought = output_buffer.getvalue()
|
|
||||||
|
|
||||||
|
result = (
|
||||||
|
output.get(langchain_object.output_keys[0])
|
||||||
|
if isinstance(output, dict)
|
||||||
|
else output
|
||||||
|
)
|
||||||
|
thought = format_actions(intermediate_steps) if intermediate_steps else ""
|
||||||
except NotEnoughElementsException as exc:
|
except NotEnoughElementsException as exc:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Error: Not enough documents for ChromaDB to index. Try reducing chunk size in TextSplitter."
|
"Error: Not enough documents for ChromaDB to index. Try reducing chunk size in TextSplitter."
|
||||||
|
|
@ -257,7 +250,7 @@ def get_result_and_thought(langchain_object, message: str):
|
||||||
else output
|
else output
|
||||||
)
|
)
|
||||||
if intermediate_steps:
|
if intermediate_steps:
|
||||||
thought = format_intermediate_steps(intermediate_steps)
|
thought = format_actions(intermediate_steps)
|
||||||
else:
|
else:
|
||||||
thought = output_buffer.getvalue()
|
thought = output_buffer.getvalue()
|
||||||
|
|
||||||
|
|
@ -266,19 +259,17 @@ def get_result_and_thought(langchain_object, message: str):
|
||||||
return result, thought
|
return result, thought
|
||||||
|
|
||||||
|
|
||||||
def format_intermediate_steps(intermediate_steps):
|
def format_actions(actions: List[Tuple[AgentAction, str]]) -> str:
|
||||||
formatted_chain = "> Entering new AgentExecutor chain...\n"
|
"""Format a list of (AgentAction, answer) tuples into a string."""
|
||||||
for step in intermediate_steps:
|
output = []
|
||||||
action = step[0]
|
for action, answer in actions:
|
||||||
observation = step[1]
|
log = action.log
|
||||||
|
tool = action.tool
|
||||||
formatted_chain += (
|
tool_input = action.tool_input
|
||||||
f" {action.log}\nAction: {action.tool}\nAction Input: {action.tool_input}\n"
|
output.append(f"Log: {log}")
|
||||||
)
|
if "Action" not in log and "Action Input" not in log:
|
||||||
formatted_chain += f"Observation: {observation}\n"
|
output.append(f"Tool: {tool}")
|
||||||
|
output.append(f"Tool Input: {tool_input}")
|
||||||
final_answer = f"Final Answer: {observation}\n"
|
output.append(f"Answer: {answer}")
|
||||||
formatted_chain += f"Thought: I now know the final answer\n{final_answer}\n"
|
output.append("") # Add a blank line
|
||||||
formatted_chain += "> Finished chain.\n"
|
return "\n".join(output)
|
||||||
|
|
||||||
return formatted_chain
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue