guardrails icon indicating copy to clipboard operation
guardrails copied to clipboard

[feat] add async guard support to langchain integration

Open dtam opened this issue 1 year ago • 11 comments

Description currently can not use AsyncGuard with the langchain integration

Why is this needed [If you have a concrete use case, add details here.]

from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

model = ChatOpenAI(model="gpt-4o")
from guardrails import AsyncGuard
from guardrails.hub import DetectPII
import asyncio

guard = AsyncGuard().use_many(
    DetectPII(pii_entities=["PERSON","LOCATION"])
)
prompt = ChatPromptTemplate.from_template("Answer this question {question}")
output_parser = StrOutputParser()

chain = prompt | model | guard.to_runnable() | output_parser

async def main():
    result = await chain.ainvoke({"question": "What are the top five airlines for domestic travel in the US?"})
    print(result)

asyncio.run(main())

Implementation details That makes sense, looks like we need overloads for Async in here: https://github.com/guardrails-ai/guardrails/blob/main/guardrails/integrations/langchain/guard_runnable.py Or probably a different AsyncGuardRunnable class

End result should execute correctly

dtam avatar Sep 03 '24 20:09 dtam

How does this fail currently?

CalebCourier avatar Sep 03 '24 20:09 CalebCourier

sys:1: RuntimeWarning: coroutine 'AsyncGuard.validate' was never awaited

dtam avatar Sep 03 '24 20:09 dtam

That makes sense, looks like we need overloads for Async in here: https://github.com/guardrails-ai/guardrails/blob/main/guardrails/integrations/langchain/guard_runnable.py Or probably a different AsyncGuardRunnable class

CalebCourier avatar Sep 03 '24 21:09 CalebCourier

is this on the roadmap?

paul-tharun avatar Oct 01 '24 09:10 paul-tharun

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Nov 01 '24 03:11 github-actions[bot]

unstale! back on the roadmap

zsimjee avatar Nov 01 '24 04:11 zsimjee

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Dec 02 '24 03:12 github-actions[bot]

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Jan 02 '25 03:01 github-actions[bot]

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Feb 02 '25 03:02 github-actions[bot]

This issue was closed because it has been stalled for 14 days with no activity.

github-actions[bot] avatar Feb 16 '25 03:02 github-actions[bot]

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Apr 19 '25 03:04 github-actions[bot]

This issue was closed because it has been stalled for 14 days with no activity.

github-actions[bot] avatar May 19 '25 03:05 github-actions[bot]