File size: 11,576 Bytes
eda43d3
ea385eb
 
e61491a
 
ea385eb
 
 
 
 
 
 
 
 
 
 
 
e61491a
 
ea385eb
eda43d3
ea385eb
eda43d3
ea385eb
eda43d3
092a0fc
eda43d3
ea385eb
 
 
eda43d3
c9742ff
039d3dc
 
e1638e4
ea385eb
eda43d3
ea385eb
eda43d3
ea385eb
eda43d3
a843da9
 
 
 
 
 
eda43d3
ea385eb
 
eda43d3
ea385eb
eda43d3
 
ea385eb
eda43d3
ea385eb
 
c9742ff
ea385eb
eda43d3
ea385eb
 
c9742ff
 
ea385eb
eda43d3
ea385eb
 
 
c9742ff
 
 
 
 
 
 
 
 
 
 
 
ea385eb
eda43d3
 
ea385eb
eda43d3
d35d94c
195089f
d05983f
195089f
c9742ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195089f
c9742ff
 
 
 
 
 
 
 
 
 
 
 
ea385eb
d35d94c
195089f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f137e4
195089f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ee1665
195089f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b9f041
ea385eb
195089f
 
 
 
 
 
 
 
26f5d5c
195089f
1b9f041
ea385eb
195089f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea385eb
4a88fde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
---
language:
- en
license: apache-2.0
library_name: transformers
tags:
- api
- open-api
- swagger
- api doc
- api call
- code
- instruction_tuned
- basemodel
- pytorch
- RL Tuned
- text-generation-inferenc
metrics:
- accuracy
pipeline_tag: text-generation
---
# pip-api-expert

[pipableAi](https://pipable.ai/)

[colab_notebook](https://colab.research.google.com/drive/1r24CjfOlCj-O0tSQYRrTBRLk9YHiHOhC?usp=sharing)

## What have we built?
A 1.3 bn state of the art model for api calling , documentation, testing management.
The tasks that the model can accomplish are the following.

```markdown
1.  Convert any bad format text to open api format.
2.  Convert any bad format text to mark down format.
3.  Given docs and questions in natural language, generate api calls in python.
```

## How we built it?

We used a simulator and a form of policy gradient to train the model to self instruct itself to make documents and then perform executable calls on the document.

## Mock interface

https://app.pipable.ai/

You can try out the features at the above interface by using our hosted model.


## License
The model is open source under apache 2.0. License

## Usage


### Installation

```bash
pip install transformers
pip install accelerate
```

### Prompt
```python
prompt = f"""<question>{}</question>
<doc/code/any tag that explains the task at hand>"""
```

### PyTorch
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from accelerate import Accelerator
import torch
path = "PipableAI/pip-api-expert"
model =AutoModelForCausalLM.from_pretrained(path,torch_dtype=torch.bfloat16,device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(path)
prompt = "<question>Perform api call to do task k</question><python_code>"
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=1200)
doc = (
    tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
)
print(doc)
```


## Examples

### Markdown Documentation Format for Raw API Docs

```python
raw_docs = """
Method access
HTTP
JavaScript
Python
Java
POST
https://slack.com/api/chat.postMessage
Required scopes
Bot tokens
chat:write
User tokens
chat:write
chat:write:user
chat:write:bot
Legacy bot tokens
bot
Content types
application/x-www-form-urlencoded
application/json
Rate limits
Special
Arguments
Required arguments
token
token
·Required
Authentication token bearing required scopes. Tokens should be passed as an HTTP Authorization header or alternatively, as a POST parameter.
Example
xxxx-xxxxxxxxx-xxxx
channel
string
·Required
Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. See below for more details.
Example
C1234567890
At least one of
attachmentsblockstext
One of these arguments is required to describe the content of the message. If attachments or blocks are included, text will be used as fallback text for notifications only.
attachments
string
blocks
blocks[] as string
text
string
How this field works and whether it is required depends on other fields you use in your API call. See below for more detail.
Example
Hello world
Optional arguments
as_user
boolean
·Optional
(Legacy) Pass true to post the message as the authed user instead of as a bot. Defaults to false. Can only be used by classic Slack apps. See authorship below.
Example
true
icon_emoji
string
·Optional
Emoji to use as the icon for this message. Overrides icon_url.
Example
:chart_with_upwards_trend:
icon_url
string
·Optional
URL to an image to use as the icon for this message.
Example
http://lorempixel.com/48/48
link_names
boolean
·Optional
Find and link user groups. No longer supports linking individual users; use syntax shown in Mentioning Users instead.
Example
true
metadata
string
·Optional
JSON object with event_type and event_payload fields, presented as a URL-encoded string. Metadata you post to Slack is accessible to any app or user who is a member of that workspace.
Example
{"event_type": "task_created", "event_payload": { "id": "11223", "title": "Redesign Homepage"}}
mrkdwn
boolean
·Optional
Disable Slack markup parsing by setting to false. Enabled by default.
Default
true
Example
false
parse
string
·Optional
Change how messages are treated. See below.
Example
full
reply_broadcast
boolean
·Optional
Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation. Defaults to false.
Example
true
thread_ts
string
·Optional
Provide another message's ts value to make this message a reply. Avoid using a reply's ts value; use its parent instead.
unfurl_links
boolean
·Optional
Pass true to enable unfurling of primarily text-based content.
Example
true
unfurl_media
boolean
·Optional
Pass false to disable unfurling of media content.
Example
false
username
string
·Optional
Set your bot's user name.
Example
My Bot
"""

question = """
Convert the above docs to markdown format.
"""

prompt = f"""
<api_doc>
{raw_docs}
</api_doc>
<question>
{question}
</question>
<response>
"""
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=1800)
doc = (
    tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
)
print(doc)
```
### OpenAPI Documentation Format for Raw API Docs

```python
raw_docs = """
Method access
HTTP
JavaScript
Python
Java
GET
https://slack.com/api/users.list
Required scopes
Bot tokens
users:read
User tokens
users:read
Legacy bot tokens
bot
Content types
application/x-www-form-urlencoded
Rate limits
Tier 2
Arguments
Required arguments
token
token
·Required
Authentication token bearing required scopes. Tokens should be passed as an HTTP Authorization header or alternatively, as a POST parameter.

Example
xxxx-xxxxxxxxx-xxxx
Optional arguments
cursor
string
·Optional
Paginate through collections of data by setting the cursor parameter to a next_cursor attribute returned by a previous request's response_metadata. Default value fetches the first "page" of the collection. See pagination for more detail.

Example
dXNlcjpVMDYxTkZUVDI=
include_locale
boolean
·Optional
Set this to true to receive the locale for users. Defaults to false

Example
true
limit
number
·Optional
The maximum number of items to return. Fewer than the requested number of items may be returned, even if the end of the users list hasn't been reached. Providing no limit value will result in Slack attempting to deliver you the entire result set. If the collection is too large you may experience limit_required or HTTP 500 errors.

Default
0
Example
20
team_id
string
·Optional
encoded team id to list users in, required if org token is used

Example
T1234567890
"""

question = """
Parse the docs to detailed openapi format, do not include reponse.
"""

prompt = f"""
<api_doc>
{raw_docs}
</api_doc>
<question>
{question}
</question>
<json>
"""
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=1800)
doc = (
    tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
)
print(doc)
```
### Python code to call an API based on the documentation and the question.

```python
docs = """
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Slack API",
    "description": "API for Slack",
    "termsOfService": "https://slack.com/terms-of-service",
    "contact": {
      "name": "Slack",
      "url": "https://slack.com",
      "email": "support@slack.com"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "https://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "servers": [
    {
      "url": "https://slack.com/api",
      "description": "API server"
    }
  ],
  "paths": {
    "/chat/postMessage": {
      "post": {
        "description": "Send a message to a channel or user",
        "parameters": [
          {
            "name": "token",
            "in": "query",
            "required": true,
            "description": "Bot token or user token",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "channel",
            "in": "query",
            "required": true,
            "description": "The ID of the channel or user to send the message to",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "text",
            "in": "query",
            "required": true,
            "description": "The message content",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "as_user",
            "in": "query",
            "required": false,
            "description": "Pass true to post the message as the authed user instead of as a bot",
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "icon_emoji",
            "in": "query",
            "required": false,
            "description": "Emoji to use as the icon for this message",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "icon_url",
            "in": "query",
            "required": false,
            "description": "URL to an image to use as the icon for this message",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "link_names",
            "in": "query",
            "required": false,
            "description": "Find and link user groups. No longer supports linking individual users; use syntax shown in Mentioning Users instead",
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "unfurl_links",
            "in": "query",
            "required": false,
            "description": "Pass true to enable unfurling of primarily text-based content",
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "unfurl_media",
            "in": "query",
            "required": false,
            "description": "Pass false to disable unfurling of media content",
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "username",
            "in": "query",
            "required": false,
            "description": "Set your bot's user name",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}
"""

instructions = f"""
- Use base url: "https://slack.com/api"
- Use above api docs.
- Use and import requests library.
- strictly show the reponse in code.
"""

question = """
Send message 'Hi, please check out https://pipable.ai.', to channel '@general'.
Use token as 'xoxb-123123123123-12312312312312-XxxxxxXXxxxx'
"""

prompt = f"""
<docs>
{docs}
</docs>
<instructions>
{instructions}
</instructions>
<question>
Write a python code for question:
{question}
</question>
<python_code>
"""

inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=500)
code = (
    tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
)
print(code)
```
### Team
Avi Kothari, Pratham Gupta, Ritvik Aryan Kalra, Soham Acharya , Gyan Ranjan