pip-api-expert / README.md
QagentS's picture
Update README.md
e1b37e1 verified
---
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