(
self,
prompt: str,
role: str = "user",
convo_id: str = "default",
**kwargs,
)
| 101 | self.conversation[convo_id].append(last_dialog) |
| 102 | |
| 103 | def ask_stream( |
| 104 | self, |
| 105 | prompt: str, |
| 106 | role: str = "user", |
| 107 | convo_id: str = "default", |
| 108 | **kwargs, |
| 109 | ) -> Generator: |
| 110 | if convo_id not in self.conversation: |
| 111 | self.reset(convo_id=convo_id) |
| 112 | self.add_to_conversation(prompt, "user", convo_id=convo_id) |
| 113 | self.__truncate_conversation(convo_id=convo_id) |
| 114 | apiKey = self.get_api_key() |
| 115 | response = self.session.post( |
| 116 | "https://api.openai.com/v1/chat/completions", |
| 117 | headers={"Authorization": f"Bearer {kwargs.get('api_key', apiKey)}"}, |
| 118 | json={ |
| 119 | "model": self.model_name, |
| 120 | "messages": self.conversation[convo_id], |
| 121 | "stream": True, |
| 122 | # kwargs |
| 123 | "temperature": kwargs.get("temperature", self.temperature), |
| 124 | "top_p": kwargs.get("top_p", self.top_p), |
| 125 | "n": kwargs.get("n", self.reply_count), |
| 126 | "user": role, |
| 127 | }, |
| 128 | stream=True, |
| 129 | ) |
| 130 | if response.status_code != 200: |
| 131 | raise Exception( |
| 132 | f"Error: {response.status_code} {response.reason} {response.text}", |
| 133 | ) |
| 134 | for line in response.iter_lines(): |
| 135 | if not line: |
| 136 | continue |
| 137 | # Remove "data: " |
| 138 | line = line.decode("utf-8")[6:] |
| 139 | if line == "[DONE]": |
| 140 | break |
| 141 | resp: dict = json.loads(line) |
| 142 | choices = resp.get("choices") |
| 143 | if not choices: |
| 144 | continue |
| 145 | delta = choices[0].get("delta") |
| 146 | if not delta: |
| 147 | continue |
| 148 | if "content" in delta: |
| 149 | content = delta["content"] |
| 150 | yield content |
| 151 | def ask(self, prompt: str, role: str = "user", convo_id: str = "default", **kwargs): |
| 152 | """ |
| 153 | Non-streaming ask |
no test coverage detected