Banana
Banana 提供无服务器 GPU 推理用于 AI 模型, 一个 CI/CD 构建管道和一个简单的 Python 框架 (
Potassium
) 来服务你的模型。
本页面介绍如何在 LangChain 中使用 Banana 生态系统。
安装和设置
- 安装 Python 包
banana-dev
:
pip install banana-dev
- 从 Banana.dev 控制面板 获取一个 Banana API 密钥,并将其设置为环境变量 (
BANANA_API_KEY
) - 从模型的详细信息页面获取您的模型密钥和 URL 别名。
定义您的 Banana 模板
您需要为您的 Banana 应用设置一 个 Github 仓库。您可以使用 本指南 在 5 分钟内开始。
或者,您可以查看 Banana 的 CodeLlama-7B-Instruct-GPTQ GitHub 仓库,以获取一个现成的 LLM 示例。只需分叉它并在 Banana 中部署。
其他入门仓库可在 这里 找到。
构建 Banana 应用
要在 LangChain 中使用 Banana 应用,您必须在返回的 JSON 中包含 outputs
键
,并且值必须是一个字符串。
# Return the results as a dictionary
result = {'outputs': result}
一个示例推理函数如下:
@app.handler("/")
def handler(context: dict, request: Request) -> Response:
"""Handle a request to generate code from a prompt."""
model = context.get("model")
tokenizer = context.get("tokenizer")
max_new_tokens = request.json.get("max_new_tokens", 512)
temperature = request.json.get("temperature", 0.7)
prompt = request.json.get("prompt")
prompt_template=f'''[INST] Write code to solve the following coding problem that obeys the constraints and passes the example test cases. Please wrap your code answer using ```:
{prompt}
[/INST]
'''
input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids.cuda()
output = model.generate(inputs=input_ids, temperature=temperature, max_new_tokens=max_new_tokens)
result = tokenizer.decode(output[0])
return Response(json={"outputs": result}, status=200)
此示例来自 CodeLlama-7B-Instruct-GPTQ 中的 app.py
文件。
大型语言模型
from langchain_community.llms import Banana
查看 使用示例。