Skip to content

Commit 6b1acdd

Browse files
committed
Add docstrings for AI tools
1 parent 27eeeae commit 6b1acdd

File tree

1 file changed

+86
-0
lines changed
  • interpreter/core/computer/ai

1 file changed

+86
-0
lines changed

interpreter/core/computer/ai/ai.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,53 @@ def query_reduce_chunks(responses, llm, chunk_size, query):
134134

135135

136136
class Ai:
137+
"""
138+
Provides AI capabilities for text processing, chat, and summarization.
139+
140+
This class offers methods to interact with the language model for various text-based tasks
141+
including chatting, querying specific information, and generating summaries.
142+
143+
Attributes:
144+
computer: The parent Computer instance this AI is attached to
145+
"""
146+
137147
def __init__(self, computer):
148+
"""
149+
Initialize an AI instance.
150+
151+
Args:
152+
computer: The parent Computer instance this AI will be attached to.
153+
Provides access to the interpreter, LLM, and other computer capabilities.
154+
155+
Example:
156+
This is typically instantiated automatically when creating a Computer instance:
157+
```python
158+
from interpreter import Computer
159+
computer = Computer() # Creates computer.ai internally
160+
```
161+
"""
138162
self.computer = computer
139163

140164
def chat(self, text, base64=None):
165+
"""
166+
Conducts a simple chat interaction with the AI.
167+
168+
This method creates a fresh chat context with minimal system instructions,
169+
useful for getting direct responses without the full computer API context.
170+
171+
Args:
172+
text (str): The message to send to the AI
173+
base64 (str, optional): Base64-encoded image data to include with the message
174+
175+
Returns:
176+
str: The AI's response text
177+
178+
Example:
179+
```python
180+
response = computer.ai.chat("What is the capital of France?")
181+
print(response) # "The capital of France is Paris."
182+
```
183+
"""
141184
messages = [
142185
{
143186
"role": "system",
@@ -185,6 +228,30 @@ def chat(self, text, base64=None):
185228
return response[-1].get("content")
186229

187230
def query(self, text, query, custom_reduce_query=None):
231+
"""
232+
Processes large text by breaking it into chunks and querying each chunk.
233+
234+
This method uses a map-reduce approach to handle large texts:
235+
1. Splits text into overlapping chunks
236+
2. Queries each chunk in parallel
237+
3. Combines the responses into a single coherent answer
238+
239+
Args:
240+
text (str): The large text to analyze
241+
query (str): The system message/query to apply to each chunk
242+
custom_reduce_query (str, optional): Custom query for the reduction phase.
243+
If None, uses the same query as chunk processing.
244+
245+
Returns:
246+
str: The final processed response
247+
248+
Example:
249+
```python
250+
text = "... very long document ..."
251+
query = "Extract all dates mentioned in the text"
252+
result = computer.ai.query(text, query)
253+
```
254+
"""
188255
if custom_reduce_query == None:
189256
custom_reduce_query = query
190257

@@ -207,6 +274,25 @@ def query(self, text, query, custom_reduce_query=None):
207274
return response
208275

209276
def summarize(self, text):
277+
"""
278+
Generates a concise summary of the provided text.
279+
280+
Uses the query method internally with pre-configured prompts optimized for
281+
summarization. Handles large texts by chunking and then combining summaries.
282+
283+
Args:
284+
text (str): The text to summarize
285+
286+
Returns:
287+
str: A concise summary of the input text
288+
289+
Example:
290+
```python
291+
long_article = "... very long article ..."
292+
summary = computer.ai.summarize(long_article)
293+
print(summary) # "This article discusses..."
294+
```
295+
"""
210296
query = "You are a highly skilled AI trained in language comprehension and summarization. I would like you to read the following text and summarize it into a concise abstract paragraph. Aim to retain the most important points, providing a coherent and readable summary that could help a person understand the main points of the discussion without needing to read the entire text. Please avoid unnecessary details or tangential points."
211297
custom_reduce_query = "You are tasked with taking multiple summarized texts and merging them into one unified and concise summary. Maintain the core essence of the content and provide a clear and comprehensive summary that encapsulates all the main points from the individual summaries."
212298
return self.query(text, query, custom_reduce_query)

0 commit comments

Comments
 (0)