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:
"""处理生成代码的请求。"""
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
文件。
LLM
from langchain_community.llms import Banana
请参见 使用示例。