Skip to content

Commit

Permalink
v0
Browse files Browse the repository at this point in the history
  • Loading branch information
yanqingmen committed Mar 9, 2023
0 parents commit 212ba19
Show file tree
Hide file tree
Showing 39 changed files with 2,113 additions and 0 deletions.
145 changes: 145 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# key files
*.key

# pycharm files
.idea
tmp
119 changes: 119 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# ask-api

#### Description
ask-api is a python framework, based on the llm models, which can help you to understand your python code by asking questions, and provide the following capabilities:
- Ask questions to understand the function.
- Execute the function directly by natural language.
- using natural language to describe the exceptions and errors.
- Other free dialogue with the function.

#### Installation (to be improved)
```shell
# pip install ask-api
```

#### Development plan (in progress)

- [x] Based on openai's chatgpt-api, support asking questions about python functions to understand the function.
- [X] Support Chinese and English prompts.
- [ ] Support custom prompts.
- [X] Support executing functions by natural language commands.
- [ ] Support other llm models or apis as the underlying algorithm support


#### Example(continuously improving)

- More details, please refer to [en_example](./examples/en_askapi_example.ipynb)
- Example function
```python
# example function, given url,download the data and return the path
import wget
import os
BASE_PATH = "./tmp/"


def download_data(url, save_path = None) -> str:
"""download data from url
Args:
url (_type_): the url of data
save_path (_type_): the path to save data
Returns:
str: the path of data
"""
if save_path is None:
save_path = wget.detect_filename(url)

if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)

save_path = f'{BASE_PATH}/{save_path}'
wget.download(url, save_path)
return save_path
```

- Use ask-api to ask questions about the function and understand the function
```python
# use ask_func to ask the function's description
session = ask_func(download_data, message="", mode='desc')

print("*" * 100)
print(session.get_current())
```
![desc_image](./docs/images/en/desc_example.png)

- use natural language to execute the function
```python
session = ask_func(download_data, message="please download this data for me:https://github.com/redis/redis/archive/7.0.9.tar.gz", mode="execute")

print(session.get_current())
```
![execute_image](./docs/images/en/execute_example.png)


- get result of the function task via wait_task
```python
# get result of the function task
from ask_api.util.askapi_asyn import wait_task

task = session.get_current().get_task()
if task is None:
print("task is complete!")
print(session.get_current())
else:
print("task is running")
result = wait_task(task)
print(result)
```
![wait_task_image](./docs/images/en/wait_task_example.png)


- handling invalid message
```python
# handling invalid message
session = ask_func(download_data, message="please download this data for me", mode="execute")

print(session.get_current())
```
![invalid_message_image](./docs/images/en/invalid_message_example.png)


- handling task exception
```python
# handling task exception
session = ask_func(download_data, message="please download this data for me:xxx.xxx", mode="execute")

print("*" * 100)
print(session.get_current())
print("*" * 100)
task = session.get_current().get_task()
if task is None:
print("task is complete!")
print(session.get_current())
else:
print("task is running")
result = wait_task(task)
print(result)
```
![task_exception_image](./docs/images/en/task_exception_example.png)

- free dialogue with the function(TODO)
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# ask-api

#### 说明
ask-api 是一个python框架,借助LLM(目前支持chatgpt-api)模型的代码理解能力,让你能够与你的python代码对话,提供包括但不限于如下的能力:
- 通过提问了解函数的用途。
- 直接通过自然语言来执行函数。
- 将操作过程中的各种异常信息,通过自然语言的方式告知用户。
- 与函数进行其他自由对话。

#### 安装(待完善)
```shell
```

#### 开发计划(进行中)

- [x] 基于openai的chatgpt-api,支持对python函数进行提问,了解函数的用途。
- [X] 支持中文以及英文两种类型的prompt。
- [ ] 支持自定义的prompt。
- [X] 支持通过自然语言命令来执行函数。
- [ ] 支持其他LLM模型或api作为底层算法支持

#### 示例(持续完善中)

- 更多细节,请参考[中文示例](./examples/cn_askapi_example.ipynb)
- 示例函数
```python
# 示例函数,给定url,下载数据并保存至指定目录
import wget
import os
BASE_PATH = "./tmp/"


def download_data(url, save_path = None) -> str:
"""下载数据并保存至指定目录
Args:
url (_type_): 需要下载的数据的url
save_path (_type_): 保存数据的目录
Returns:
str: 保存数据的目录
"""
if save_path is None:
save_path = wget.detect_filename(url)

if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)

save_path = f'{BASE_PATH}/{save_path}'
wget.download(url, save_path)
return save_path
```

- 利用ask-api对函数进行提问,了解函数的用途
```python
from ask_api.ask_api import ask_func

# 获取函数的功能说明
session = ask_func(download_data, message="", mode='desc')

print("*" * 100)
print(session.get_current())
```
![desc_image](./docs/images/zh/desc_example.png)

- 利用ask-api,直接通过自然语言来执行函数
```python
# 使用自然语言调用函数
session = ask_func(download_data, message="请帮我下载这个数据:https://github.com/redis/redis/archive/7.0.9.tar.gz", mode="execute")

print(session.get_current())
```
![execute_image](./docs/images/zh/execute_example.png)

- 函数执行为异步执行,可以通过wait_task方法等待函数执行完成
```python
# get result of the function task
from ask_api.util.askapi_asyn import wait_task

task = session.get_current().get_task()
if task is None:
print("task is complete!")
print(session.get_current())
else:
print("task is running")
result = wait_task(task)
print(result)
```
![wait_task_image](./docs/images/zh/wait_task_example.png)

- 异常命令的处理
```python
# 异常指令处理
session = ask_func(download_data, message="请帮我下载", mode="execute")

print(session.get_current())
```
![invalid_message_image](./docs/images/zh/invalid_message.png)

- 函数执行异常的处理
```python
# 任务执行中异常处理
session = ask_func(download_data, message="请帮我下载这个数据:xxx.xxx", mode="execute")

print("*" * 100)
print(session.get_current())
print("*" * 100)
task = session.get_current().get_task()
result_message = wait_task(task)
print(result_message)
```
![error_task_image](./docs/images/zh/error_task_example.png)

- 与函数进行其他自由对话(TODO)
Binary file added docs/images/en/desc_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/en/error_task_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/en/execute_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/en/invalid_message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/en/wait_task_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/zh/desc_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/zh/error_task_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/zh/execute_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/zh/invalid_message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/zh/wait_task_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 212ba19

Please sign in to comment.