-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunctions.py
107 lines (82 loc) · 2.86 KB
/
functions.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""A module for running OpenAI functions"""
from __future__ import annotations
from dataclasses import dataclass
import json
from typing import Any, Protocol, TYPE_CHECKING, runtime_checkable
from ..exceptions import NonSerializableOutputError
if TYPE_CHECKING:
from ..json_type import JsonType
@runtime_checkable
class OpenAIFunction(Protocol):
"""A protocol for OpenAI functions.
Requires a __call__ method, a schema property, and a name property,
as well as those that define the treatment of the return value.
"""
def __call__(self, arguments: dict[str, JsonType]) -> Any:
...
@property
def schema(self) -> JsonType:
"""Get the schema for this function"""
@property
def name(self) -> str:
"""Get the name of this function"""
# This ellipsis is for Pyright #2758
... # pylint: disable=unnecessary-ellipsis
@property
def save_return(self) -> bool:
"""Get whether to save the return value of this function"""
... # pylint: disable=unnecessary-ellipsis
@property
def serialize(self) -> bool:
"""Get whether to continue running after this function"""
... # pylint: disable=unnecessary-ellipsis
@property
def remove_call(self) -> bool:
"""Get whether to remove the call to this function from the chat history"""
... # pylint: disable=unnecessary-ellipsis
@property
def interpret_as_response(self) -> bool:
"""Get whether to interpret the return value of this function as a response"""
... # pylint: disable=unnecessary-ellipsis
@dataclass
class RawFunctionResult:
"""A raw function result"""
result: Any
serialize: bool = True
@property
def serialized(self) -> str:
"""Get the serialized result
Raises:
NonSerializableOutputError: If the result cannot be serialized
Returns:
str: The serialized result
"""
if self.serialize:
try:
return json.dumps(self.result)
except TypeError as error:
raise NonSerializableOutputError(self.result) from error
return str(self.result)
@dataclass
class FunctionResult:
"""A result of a function's execution"""
name: str
raw_result: RawFunctionResult | None
remove_call: bool = False
interpret_return_as_response: bool = False
@property
def content(self) -> str | None:
"""Get the content of this result
Returns:
str | None: The content
"""
return self.raw_result.serialized if self.raw_result else None
@property
def result(self) -> Any | None:
"""Get the result of this function call
Returns:
The raw result of the function call
"""
if self.raw_result:
return self.raw_result.result
return None