1
1
"""Class definition for SimpleBot."""
2
2
import contextvars
3
- from typing import Optional
3
+ from typing import Optional , Union
4
4
5
5
6
6
from llamabot .components .messages import (
@@ -29,6 +29,8 @@ class SimpleBot:
29
29
:param model_name: The name of the model to use.
30
30
:param stream: Whether to stream the output to stdout.
31
31
:param json_mode: Whether to print debug messages.
32
+ :param api_key: The OpenAI API key to use.
33
+ :param mock_response: A mock response to use, for testing purposes only.
32
34
"""
33
35
34
36
def __init__ (
@@ -39,56 +41,81 @@ def __init__(
39
41
stream = True ,
40
42
json_mode : bool = False ,
41
43
api_key : Optional [str ] = None ,
44
+ mock_response : Optional [str ] = None ,
42
45
):
43
46
self .system_prompt : SystemMessage = SystemMessage (content = system_prompt )
44
47
self .model_name = model_name
45
48
self .temperature = temperature
46
49
self .stream = stream
47
50
self .json_mode = json_mode
48
51
self .api_key = api_key
52
+ self .mock_response = mock_response
49
53
50
- def __call__ (self , human_message : str ) -> AIMessage :
54
+ def __call__ (self , human_message : str ) -> Union [ AIMessage , str ] :
51
55
"""Call the SimpleBot.
52
56
53
57
:param human_message: The human message to use.
54
58
:return: The response to the human message, primed by the system prompt.
55
59
"""
56
60
57
- messages : list [BaseMessage ] = [
58
- self .system_prompt ,
59
- HumanMessage (content = human_message ),
60
- ]
61
+ messages = [self .system_prompt , HumanMessage (content = human_message )]
62
+ if self .stream :
63
+ return self .stream_response (messages )
61
64
response = self .generate_response (messages )
62
65
autorecord (human_message , response .content )
63
66
return response
64
67
65
68
def generate_response (self , messages : list [BaseMessage ]) -> AIMessage :
66
- """Generate a response from the given messages."""
67
-
68
- messages_dumped : list [dict ] = [m .model_dump () for m in messages ]
69
- completion_kwargs = dict (
70
- model = self .model_name ,
71
- messages = messages_dumped ,
72
- temperature = self .temperature ,
73
- stream = self .stream ,
74
- )
75
- if self .json_mode :
76
- completion_kwargs ["response_format" ] = {"type" : "json_object" }
77
- if self .api_key :
78
- completion_kwargs ["api_key" ] = self .api_key
79
- response = completion (** completion_kwargs )
69
+ """Generate a response from the given messages.
80
70
81
- if self .stream :
82
- ai_message = ""
83
- for chunk in response :
84
- delta = chunk .choices [0 ].delta .content
85
- if delta is not None :
86
- print (delta , end = "" )
87
- ai_message += delta
88
- return AIMessage (content = ai_message )
71
+ :param messages: A list of messages.
72
+ :return: The response to the messages.
73
+ """
89
74
75
+ response = _make_response (self , messages )
90
76
return AIMessage (content = response .choices [0 ].message .content )
91
77
78
+ def stream_response (self , messages : list [BaseMessage ]) -> str :
79
+ """Stream the response from the given messages.
80
+
81
+ This is intended to be used with Panel's ChatInterface as part of the callback.
82
+
83
+ :param messages: A list of messages.
84
+ :return: A generator that yields the response.
85
+ """
86
+ response = _make_response (self , messages )
87
+ message = ""
88
+ for chunk in response :
89
+ delta = chunk .choices [0 ].delta .content
90
+ if delta is not None :
91
+ message += delta
92
+ print (delta , end = "" )
93
+ yield message
94
+ print ()
95
+
96
+
97
+ def _make_response (bot : SimpleBot , messages : list [BaseMessage ]):
98
+ """Make a response from the given messages.
99
+
100
+ :param bot: A SimpleBot
101
+ :param messages: A list of Messages.
102
+ :return: A response object.
103
+ """
104
+ messages_dumped : list [dict ] = [m .model_dump () for m in messages ]
105
+ completion_kwargs = dict (
106
+ model = bot .model_name ,
107
+ messages = messages_dumped ,
108
+ temperature = bot .temperature ,
109
+ stream = bot .stream ,
110
+ )
111
+ if bot .mock_response :
112
+ completion_kwargs ["mock_response" ] = bot .mock_response
113
+ if bot .json_mode :
114
+ completion_kwargs ["response_format" ] = {"type" : "json_object" }
115
+ if bot .api_key :
116
+ completion_kwargs ["api_key" ] = bot .api_key
117
+ return completion (** completion_kwargs )
118
+
92
119
# Commented out until later.
93
120
# def panel(
94
121
# self,
0 commit comments