Skip to content

Commit 6e61338

Browse files
committed
Add SingleKeyDictOutputParser
1 parent 6e39272 commit 6e61338

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ads/llm/parsers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*--
3+
4+
# Copyright (c) 2023 Oracle and/or its affiliates.
5+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
6+
7+
8+
from typing import Any
9+
from langchain.schema.output_parser import BaseOutputParser
10+
11+
12+
class SingleKeyDictOutputParser(BaseOutputParser[Any]):
13+
"""Stores the text into a dictionary with a single key.
14+
15+
Example::
16+
17+
# Putting the output of the LLM into a dictionary
18+
chain = GenerativeAI(...) | SingleKeyDictOutputParser(key="my_key")
19+
# The ``output`` will be a dictionary like ``{"my_key": "..."}``
20+
output = chain.invoke("...")
21+
22+
"""
23+
24+
key: str
25+
"""The key in the dictionary. The the input text will be store as the value of this key."""
26+
27+
def parse(self, text: str) -> dict:
28+
return {self.key: text}

0 commit comments

Comments
 (0)