Skip to main content
Banana 为 AI 模型提供无服务器 GPU 推理服务, 配备 CI/CD 构建流水线和简单的 Python 框架(Potassium)用于模型服务。
本页介绍如何在 LangChain 中使用 Banana 生态系统。

安装与设置

  • 安装 Python 包 banana-dev
pip install banana-dev
  • Banana.dev 仪表板获取 Banana API 密钥,并将其设置为环境变量(BANANA_API_KEY
  • 从模型详情页面获取模型密钥和 URL slug。

定义您的 Banana 模板

您需要为您的 Banana 应用程序设置一个 GitHub 仓库。您可以使用此指南在 5 分钟内上手。 或者,如果您想直接使用现成的 LLM 示例,可以查看 Banana 的 CodeLlama-7B-Instruct-GPTQ GitHub 仓库。只需 Fork 并在 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 文件。

LLM

from langchain_community.llms import Banana
查看使用示例