香蕉
香蕉 提供了用于 AI 模型的无服务器 GPU 推理、CI/CD 构建流水线以及一个简单的 Python 框架(Potassium
)来为您的模型提供服务。
本页面介绍了如何在 LangChain 中使用 香蕉 生态系统。
安装和设置
- 安装 Python 包
banana-dev
:
pip install banana-dev
从 香蕉.dev 仪表板 获取一个香蕉 API 密钥,并将其设置为环境变量(
BANANA_API_KEY
)从模型详情页面获取您的模型密钥和 URL 别名。
定义您的香蕉模板
您需要为您的香蕉应用程序设置一个 Github 存储库。您可以使用此指南在 5 分钟内开始。
或者,对于一个现成的 LLM 示例,您可以查看香蕉的 CodeLlama-7B-Instruct-GPTQ GitHub 存储库。只需 fork 它并在香蕉中部署它。
其他起始存储库可在此处找到。
构建香蕉应用程序
要在 Langchain 中使用香蕉应用程序,您必须在返回的 JSON 中包含 outputs
键,并且该值必须是一个字符串。
# 将结果作为字典返回
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
查看使用示例。