Langchain如何调用Siliconflow的模型
硅基流动之前宣布了部分模型免费,那我们就可以调用API来搞些事情。
官网(Chat Completions (siliconflow.cn))上给出了详细的调用方式,我们可以方便的ctrl C 和 ctrl V
而Langchain支持自定义封装LLM(Custom LLM | 🦜️🔗 Langchain)
那我们就可以结合搞点事情,封装好的代码在这,需要自取:
from langchain.llms.base import LLM
from langchain_community.llms.utils import enforce_stop_tokens
import requests
import os
# 设置API密钥和基础URL环境变量
API_KEY = os.getenv("CUSTOM_API_KEY", "<Your Key>")
BASE_URL = "https://api.siliconflow.cn/v1/chat/completions"
class SiliconFlow(LLM):
def __init__(self):
super().__init__()
@property
def _llm_type(self) -> str:
return "siliconflow"
def siliconflow_completions(self, model: str, prompt: str) -> str:
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"stream": False
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {API_KEY}"
}
response = requests.post(BASE_URL, json=payload, headers=headers)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
def _call(self, prompt: str, stop: list = None, model: str = "default-model") -> str:
response = self.siliconflow_completions(model=model, prompt=prompt)
if stop is not None:
response = enforce_stop_tokens(response, stop)
return response
if __name__ == "__main__":
llm = SiliconFlow()
response = llm._call(prompt="<Prompt>", model="<Model>")
print(response)
作者:LJY_LU