Skip to content

Commit

Permalink
[AIC-py] usability fixes for llama cookbook
Browse files Browse the repository at this point in the history
Summary
- expose CLI args for paths for easier usage
- use extension pypi package
- add readme for installation + usage
  • Loading branch information
jonathanlastmileai committed Nov 17, 2023
1 parent 70d8fe2 commit db15f4d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
16 changes: 16 additions & 0 deletions cookbooks/llama/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Example app using llama aiconfig extension.

1. Install (example using anaconda)
`conda create -n aiconfig-llama-cookbook`
`conda activate aiconfig-llama-cookbook`
`conda install pip`
`pip install -r python/requirements.txt`

2. Download a model, e.g.
`curl -L https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_K_M.gguf --output ./models/llama-2-7b-chat.Q4_K_M.gguf`
3. cd into cookbook root dir
`$ pwd`
aiconfig/cookbooks/llama
4. Create an AIConfig like this: https://github.com/lastmile-ai/aiconfig/blob/e92e5a3c80b9c2b74a9432f0441318a951d54d0c/cookbooks/llama/llama-aiconfig.json
5. Run with your local paths:
`python python/ask_llama.py --aiconfig-path='../llama/llama-aiconfig.json' --model-path='../../models/llama-2-7b-chat.Q4_K_M.gguf' 2> ask-llama.err`
30 changes: 24 additions & 6 deletions cookbooks/llama/python/ask_llama.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import asyncio
import sys

from aiconfig.model_parser import InferenceOptions
from llama import LlamaModelParser

from aiconfig import AIConfigRuntime
import argparse


async def main():
llama_model_parser = LlamaModelParser(
model_path="models/llama-2-7b-chat.Q4_K_M.gguf"
parser = argparse.ArgumentParser()
parser.add_argument(
"--aiconfig-path",
type=str,
required=True,
help="Relative or absolute path to aiconfig json, e.g. cookbooks/llama/llama-aiconfig.json",
)
parser.add_argument(
"--model-path",
type=str,
required=True,
help="Relative or absolute path to model",
)
args = parser.parse_args()
return await run(args.aiconfig_path, args.model_path)


async def run(aiconfig_path: str, model_path: str):
llama_model_parser = LlamaModelParser(model_path=model_path)

for lm in [
"llama-2-7b-chat",
Expand All @@ -18,7 +36,7 @@ async def main():
]:
AIConfigRuntime.register_model_parser(llama_model_parser, lm)

config = AIConfigRuntime.load("cookbooks/llama/llama-aiconfig.json")
config = AIConfigRuntime.load(aiconfig_path)

def stream_callback(data, accumulated_message, index):
print(data, end="", flush=True)
Expand All @@ -38,9 +56,9 @@ def stream_callback(data, accumulated_message, index):
await config.run("prompt13b", params={}, options=inference_options)

print("\n\nRunning prompt13b_code...")
code_res = await config.run("prompt13b_code", params={}, options=inference_options)
print(f"\n\n\n\nCode response:\n{code_res}")
await config.run("prompt13b_code", params={}, options=inference_options)


if __name__ == "__main__":
asyncio.run(main())
res = asyncio.run(main())
sys.exit(res)
43 changes: 43 additions & 0 deletions cookbooks/llama/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[build-system]
requires = ["setuptools", "wheel"]

[project]
name = "python-aiconfig-llama-cookbook"
version = "0.0.1"
authors = [
{ name="Jonathan Lessinger", email="[email protected]" },
]
description = "LLama cookbook using AIConfig Library"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
]
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[project.urls]
"Homepage" = "https://github.com/lastmile-ai/aiconfig"
"Bug Tracker" = "https://github.com/lastmile-ai/aiconfig/issues"

# Black formatting
[tool.black]
line-length = 99
include = '\.pyi?$'
exclude = '''
/(
.eggs # exclude a few common directories in the
| .git # root of the project
| .hg
| .mypy_cache
| .tox
| venv
| _build
| buck-out
| build
| dist
)/
'''
2 changes: 2 additions & 0 deletions cookbooks/llama/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-aiconfig-llama
python-aiconfig

0 comments on commit db15f4d

Please sign in to comment.