guardrails-ai / guardrails

Adding guardrails to large language models.
https://www.guardrailsai.com/docs
Apache License 2.0
4.19k stars 319 forks source link

[feat] add async guard support to langchain integration #1046

Open dtam opened 3 months ago

dtam commented 3 months ago

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

CalebCourier commented 3 months ago

How does this fail currently?

dtam commented 3 months ago

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

CalebCourier commented 3 months ago

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

paul-tharun commented 2 months ago

is this on the roadmap?

github-actions[bot] commented 1 month ago

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.

zsimjee commented 1 month ago

unstale! back on the roadmap

github-actions[bot] commented 1 day ago

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.