|
1 | 1 | from requests.exceptions import HTTPError |
2 | | -from typing import List, Union |
| 2 | +from typing import Iterable, List, Union |
3 | 3 | from urllib.parse import urlparse |
4 | 4 | from uuid import uuid4 |
5 | 5 | import datetime |
@@ -37,6 +37,12 @@ class Agent: |
37 | 37 | >>> completion = agent.completion([{'question': 'What is your name?', 'answer': None}]) |
38 | 38 | >>> print(completion.content) |
39 | 39 |
|
| 40 | + Query an agent with streaming: |
| 41 | +
|
| 42 | + >>> completion = agent.completion_stream([{'question': 'What is your name?', 'answer': None}]) |
| 43 | + >>> for chunk in completion: |
| 44 | + print(chunk.choices[0].delta.content) |
| 45 | +
|
40 | 46 | List all agents: |
41 | 47 |
|
42 | 48 | >>> agents = agents.list() |
@@ -81,6 +87,9 @@ def __init__( |
81 | 87 | def completion(self, messages: List[dict]) -> AgentCompletion: |
82 | 88 | return self.collection.completion(self.name, messages) |
83 | 89 |
|
| 90 | + def completion_stream(self, messages: List[dict]) -> Iterable[object]: |
| 91 | + return self.collection.completion_stream(self.name, messages) |
| 92 | + |
84 | 93 | def add_files(self, file_paths: List[str], description: str, knowledge_base: str = None): |
85 | 94 | """ |
86 | 95 | Add a list of files to the agent for retrieval. |
@@ -195,6 +204,17 @@ def completion(self, name: str, messages: List[dict]) -> AgentCompletion: |
195 | 204 | data = self.api.agent_completion(self.project, name, messages) |
196 | 205 | return AgentCompletion(data['message']['content']) |
197 | 206 |
|
| 207 | + def completion_stream(self, name, messages: List[dict]) -> Iterable[object]: |
| 208 | + """ |
| 209 | + Queries the agent for a completion and streams the response as an iterable object. |
| 210 | +
|
| 211 | + :param name: Name of the agent |
| 212 | + :param messageS: List of messages to be sent to the agent |
| 213 | +
|
| 214 | + :return: iterable of completion chunks from querying the agent. |
| 215 | + """ |
| 216 | + return self.api.agent_completion_stream(self.project, name, messages) |
| 217 | + |
198 | 218 | def _create_default_knowledge_base(self, agent: Agent, name: str) -> KnowledgeBase: |
199 | 219 | # Make sure default ML engine for embeddings exists. |
200 | 220 | try: |
|
0 commit comments