-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.py
39 lines (30 loc) · 984 Bytes
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""`core.py`"""
from typing import Any
from langchain.output_parsers import PydanticOutputParser
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import BaseMessage
async def run(
llm:BaseChatModel,
prompt:list[BaseMessage],
parser:PydanticOutputParser
)->Any:
_output = None
output = None
# Requesting LLM
# ------------------
try:
_output = await llm.ainvoke(prompt)
except BaseException as e:
print(f"Error in LLM communication {e}")
# Parsing/Formatting the output
# ------------------
try:
if _output is not None:
output = parser.parse(_output.content)
except BaseException as e:
print(f"Error in parsing {e}")
# Backup in case parsing fails
# --------------------------------
if isinstance(_output, BaseMessage):
output = _output.content
return output