55import warnings
66
77
8+ def build (task_description : str , is_async : bool = False , api_key : Optional [str ] = None ) -> "AIFunction" :
9+ """
10+ Build a specialized AI function for a given task.
11+
12+ Parameters
13+ ----------
14+ task_description : str
15+ The description of the task for which the function is being built.
16+
17+ is_async : bool, optional
18+ Indicates whether the function should be asynchronous. Defaults to False.
19+
20+ api_key : str, optional
21+ The API key for the WecoAI service. If not provided, the API key must be set using the environment variable - `WECO_API_KEY`.
22+
23+ Returns
24+ -------
25+ AIFunction
26+ A specialized AI function for the given task.
27+
28+ Examples
29+ --------
30+ >>> from aifn import build
31+ >>> country_to_capital = build("Given a country name, return its capital city as 'capital'.")
32+ >>> country_to_capital("India").output["capital"]
33+ 'New Delhi'
34+ """
35+ client = Client (api_key = api_key )
36+ fn_name , fn_version , fn_desc = client .build (task_description = task_description )
37+ return AIFunction (fn_name = fn_name , version = fn_version , fn_desc = fn_desc , is_async = is_async , api_key = api_key )
38+
39+
840class AIFunction :
941 """
1042 An AI powered function that can be called like any other function to perform a specific task.
@@ -83,6 +115,33 @@ def __call__(
83115 -------
84116 NamedTuple
85117 A NamedTuple containing the output and metadata of the response.
118+
119+ Examples
120+ --------
121+
122+ **Build and call a function**
123+
124+ >>> from aifn import build
125+ >>> country_to_capital = build("Given a country name, return its capital city as 'capital'.")
126+ >>> response = country_to_capital("France")
127+ >>> response.output["capital"]
128+ 'Paris'
129+
130+ **Retrieve and call an existing function**
131+
132+ >>> from aifn import AIFunction
133+ >>> idea_evaluator = AIFunction("BusinessIdeaAnalyzer-XYZ123")
134+ >>> response = idea_evaluator("A platform to connect pet owners with pet sitters.")
135+ >>> response.output["score"]
136+ 0.85
137+
138+ **Call an existing function with image inputs**
139+
140+ >>> from aifn import AIFunction
141+ >>> image_classifier = AIFunction("ImageClassifier-ABC123")
142+ >>> response = image_classifier(images_input=["https://example.com/cat.jpg"])
143+ >>> response.output["label"]
144+ 'cat'
86145 """
87146 return self ._fn (
88147 fn_name = self .fn_name ,
@@ -119,6 +178,16 @@ def batch(
119178 -------
120179 List[NamedTuple]
121180 A list of NamedTuples, each containing the output and metadata of the response.
181+
182+ Examples
183+ -------
184+ >>> from aifn import build
185+ >>> country_to_capital = build("Given a country name, return its capital city as 'capital'.")
186+ >>> batch_inputs = [{"text_input": "India"}, {"text_input": "USA"}, {"text_input": "UK"}]
187+ >>> responses = country_to_capital.batch(batch_inputs)
188+ >>> outputs = [response.output["capital"] for response in responses]
189+ >>> outputs
190+ ['New Delhi', 'Washington, D.C.', 'London']
122191 """
123192 return self ._batch_fn (
124193 fn_name = self .fn_name ,
@@ -179,12 +248,21 @@ async def async_wrapper(*args, **kwargs):
179248
180249 def make_sync (self ) -> "AIFunction" :
181250 """
182- Convert the AI function to synchronous mode .
251+ Convert an asynchronous AI function to a synchronous AI Function .
183252
184253 Returns
185254 -------
186255 AIFunction
187256 A new AIFunction instance in synchronous mode.
257+
258+ Examples
259+ -------
260+ >>> from aifn import build
261+ >>> country_to_capital = build("Given a country name, return its capital city as 'capital'.", is_async=True)
262+ >>> sync_country_to_capital = country_to_capital.make_sync()
263+ >>> response = sync_country_to_capital("USA")
264+ >>> response.output["capital"]
265+ 'Washington, D.C.'
188266 """
189267 if not self .is_async :
190268 warnings .warn (f"{ self } is already a synchronous AI Function...Returning the same object." )
@@ -195,41 +273,25 @@ def make_sync(self) -> "AIFunction":
195273
196274 def make_async (self ) -> "AIFunction" :
197275 """
198- Convert the AI function to asynchronous mode .
276+ Convert a synchronous AI function to an asynchronous AI Function .
199277
200278 Returns
201279 -------
202280 AIFunction
203281 A new AIFunction instance in asynchronous mode.
282+
283+ Examples
284+ -------
285+ >>> from aifn import build
286+ >>> country_to_capital = build("Given a country name, return its capital city as 'capital'.", is_async=False)
287+ >>> async_country_to_capital = country_to_capital.make_async()
288+ >>> response = await async_country_to_capital("USA")
289+ >>> response.output["capital"]
290+ 'Washington, D.C.'
204291 """
205292 if self .is_async :
206293 warnings .warn (f"{ self } is already an asynchronous AI Function...Returning the same object." )
207294 return self
208295 return AIFunction (
209296 fn_name = self .fn_name , version = self .version , fn_desc = self .fn_desc , is_async = True , api_key = self ._client .api_key
210297 )
211-
212-
213- def build (task_description : str , is_async : bool = False , api_key : Optional [str ] = None ) -> AIFunction :
214- """
215- Build a specialized AI function for a given task.
216-
217- Parameters
218- ----------
219- task_description : str
220- The description of the task for which the function is being built.
221-
222- is_async : bool, optional
223- Indicates whether the function should be asynchronous. Defaults to False.
224-
225- api_key : str, optional
226- The API key for the WecoAI service. If not provided, the API key must be set using the environment variable - `WECO_API_KEY`.
227-
228- Returns
229- -------
230- AIFunction
231- A specialized AI function for the given task.
232- """
233- client = Client (api_key = api_key )
234- fn_name , fn_version , fn_desc = client .build (task_description = task_description )
235- return AIFunction (fn_name = fn_name , version = fn_version , fn_desc = fn_desc , is_async = is_async , api_key = api_key )
0 commit comments