Banana
Banana 為 AI 模型提供無伺服器 GPU 推論、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 分鐘內開始使用。
或者,對於現成的 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
API 參考:Banana
請參閱用法範例。