当前位置: 首页 > news >正文

langflow中添加Siliconflow组件

在这里插入图片描述
在这里插入图片描述

组件代码:

import requests
from pydantic.v1 import SecretStr
from typing_extensions import override
 
from langflow.base.models.model  import LCModelComponent
from langflow.field_typing  import LanguageModel 
from langflow.field_typing.range_spec  import RangeSpec 
from langflow.inputs  import BoolInput, DictInput, DropdownInput, IntInput, SecretStrInput, SliderInput, StrInput
 
# 定义 SiliconFlow 提供的模型列表
SILICONFLOW_MODELS = [
    "Pro/deepseek-ai/DeepSeek-R1",
    "Pro/deepseek-ai/DeepSeek-V3",
    "deepseek-ai/DeepSeek-R1",
    "deepseek-ai/DeepSeek-V3",
    "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
    "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
    "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B",
    "deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
    "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
    "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
    "SeedLLM/Seed-Rice-7B",
    "Qwen/QVQ-72B-Preview",
    "deepseek-ai/DeepSeek-V2.5",
    "meta-llama/Llama-3.3-70B-Instruct",
    "Qwen/QwQ-32B-Preview",
    "Qwen/Qwen2.5-Coder-32B-Instruct",
    "Qwen/Qwen2-VL-72B-Instruct",
    "OpenGVLab/InternVL2-26B",
    "Qwen/Qwen2.5-72B-Instruct-128K",
    "deepseek-ai/deepseek-vl2",
    "Qwen/Qwen2.5-72B-Instruct",
    "Qwen/Qwen2.5-32B-Instruct",
    "Qwen/Qwen2.5-14B-Instruct",
    "Qwen/Qwen2.5-7B-Instruct",
    "Qwen/Qwen2.5-Coder-7B-Instruct",
    "Qwen/Qwen2-VL-7B-Instruct",
    "OpenGVLab/InternVL2-8B",
    "Qwen/Qwen2-7B-Instruct",
    "Qwen/Qwen2-1.5B-Instruct",
    "THUDM/glm-4-9b-chat",
    "THUDM/chatglm3-6b",
    "01-ai/Yi-1.5-9B-Chat-16K",
    "01-ai/Yi-1.5-6B-Chat",
    "01-ai/Yi-1.5-34B-Chat-16K",
    "google/gemma-2-27b-it",
    "google/gemma-2-9b-it",
    "AIDC-AI/Marco-o1",
    "LoRA/meta-llama/Meta-Llama-3.1-8B-Instruct",
    "LoRA/Qwen/Qwen2.5-32B-Instruct",
    "LoRA/Qwen/Qwen2.5-14B-Instruct",
    "Vendor-A/Qwen/Qwen2.5-72B-Instruct",
    "Pro/deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
    "Pro/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
    "Pro/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
    "Pro/Qwen/Qwen2.5-Coder-7B-Instruct",
    "Pro/Qwen/Qwen2-VL-7B-Instruct",
    "Pro/OpenGVLab/InternVL2-8B",
    "Pro/Qwen/Qwen2.5-7B-Instruct",
    "Pro/meta-llama/Meta-Llama-3.1-8B-Instruct",
    "LoRA/Qwen/Qwen2.5-72B-Instruct",
    "Pro/Qwen/Qwen2-7B-Instruct",
    "Pro/Qwen/Qwen2-1.5B-Instruct",
    "LoRA/Qwen/Qwen2.5-7B-Instruct",
    "Pro/THUDM/glm-4-9b-chat",
    "Pro/google/gemma-2-9b-it"
]
 
class SiliconFlowModelComponent(LCModelComponent):
    display_name = "SiliconFlow"
    description = "Generate text using SiliconFlow LLMs."
    icon = "google"
 
    inputs = [
        *LCModelComponent._base_inputs,
        IntInput(
            name="max_tokens",
            display_name="Max Tokens",
            advanced=True,
            info="Maximum number of tokens to generate. Set to 0 for unlimited.",
            range_spec=RangeSpec(min=0, max=128000),
        ),
        DictInput(
            name="model_kwargs",
            display_name="Model Kwargs",
            advanced=True,
            info="Additional keyword arguments to pass to the model.",
        ),
        BoolInput(
            name="json_mode",
            display_name="JSON Mode",
            advanced=True,
            info="If True, it will output JSON regardless of passing a schema.",
        ),
        DropdownInput(
            name="model_name",
            display_name="Model Name",
            info="SiliconFlow model to use",
            options=SILICONFLOW_MODELS,
            value="deepseek-ai/DeepSeek-R1",
            refresh_button=True,
        ),
        StrInput(
            name="api_base",
            display_name="SiliconFlow API Base",
            advanced=True,
            info="Base URL for API requests. Defaults to https://api.siliconflow.cn", 
            value="https://api.siliconflow.cn", 
        ),
        SecretStrInput(
            name="api_key",
            display_name="SiliconFlow API Key",
            info="The SiliconFlow API Key",
            advanced=False,
            required=True,
        ),
        SliderInput(
            name="temperature",
            display_name="Temperature",
            info="Controls randomness in responses",
            value=1.0,
            range_spec=RangeSpec(min=0, max=2, step=0.01),
        ),
        IntInput(
            name="seed",
            display_name="Seed",
            info="The seed controls the reproducibility of the job.",
            advanced=True,
            value=1,
        ),
    ]
 
    def get_models(self) -> list[str]:
        if not self.api_key: 
            return SILICONFLOW_MODELS 
 
        url = f"{self.api_base}/models" 
        headers = {"Authorization": f"Bearer {self.api_key}",  "Accept": "application/json"}
 
        try:
            response = requests.get(url,  headers=headers, timeout=10)
            response.raise_for_status() 
            model_list = response.json() 
            return [model["id"] for model in model_list.get("data",  [])]
        except requests.RequestException as e:
            self.status  = f"Error fetching models: {e}"
            return SILICONFLOW_MODELS
 
    @override
    def update_build_config(self, build_config: dict, field_value: str, field_name: str | None = None):
        if field_name in {"api_key", "api_base", "model_name"}:
            models = self.get_models() 
            build_config["model_name"]["options"] = models
        return build_config
 
    def build_model(self) -> LanguageModel:
        try:
            from langchain_openai import ChatOpenAI
        except ImportError as e:
            msg = "langchain-openai not installed. Please install with `pip install langchain-openai`"
            raise ImportError(msg) from e
 
        api_key = SecretStr(self.api_key).get_secret_value()  if self.api_key  else None 
        output = ChatOpenAI(
            model=self.model_name, 
            temperature=self.temperature  if self.temperature  is not None else 0.1,
            max_tokens=self.max_tokens  or None,
            model_kwargs=self.model_kwargs  or {},
            base_url=self.api_base, 
            api_key=api_key,
            streaming=self.stream  if hasattr(self, "stream") else False,
            seed=self.seed, 
        )
 
        if self.json_mode: 
            output = output.bind(response_format={"type":  "json_object"})
 
        return output
 
    def _get_exception_message(self, e: Exception):
        """Get message from SiliconFlow API exception."""
        try:
            from openai import BadRequestError
 
            if isinstance(e, BadRequestError):
                message = e.body.get("message") 
                if message:
                    return message 
        except ImportError:
            pass
        return None

文章转载自:

http://eJRY8S3z.cfccp.cn
http://149wdv4w.cfccp.cn
http://5n7W6wyw.cfccp.cn
http://ICOcujot.cfccp.cn
http://Deaz0ePE.cfccp.cn
http://sQcSgLvV.cfccp.cn
http://ciFxJ4En.cfccp.cn
http://9mwz1MN1.cfccp.cn
http://gRD4pr6y.cfccp.cn
http://6ocbhzOQ.cfccp.cn
http://JjB3EWyR.cfccp.cn
http://59psUiPq.cfccp.cn
http://ifNPMPwk.cfccp.cn
http://LjJEwhpk.cfccp.cn
http://O4eLmxRp.cfccp.cn
http://ODQDrq7A.cfccp.cn
http://h5URsX2X.cfccp.cn
http://0ACtGkHh.cfccp.cn
http://Ral2VMI0.cfccp.cn
http://1RBYdDV6.cfccp.cn
http://OIZO53Rn.cfccp.cn
http://HCIlX1OO.cfccp.cn
http://W28rzHY7.cfccp.cn
http://EnGAu4fW.cfccp.cn
http://XQ166B1A.cfccp.cn
http://gMdNwxhc.cfccp.cn
http://mh3PPL6D.cfccp.cn
http://cLtkOiMj.cfccp.cn
http://AUXyDdsV.cfccp.cn
http://l0MwYjBE.cfccp.cn
http://www.dtcms.com/a/28578.html

相关文章:

  • 拆解微软CEO纳德拉战略蓝图:AI、量子计算、游戏革命如何改写未来规则!
  • DAY01-如何合理配置线程池的核心参数
  • 【机器学习】衡量线性回归算法最好的指标:R Squared
  • js中常用方法整理
  • 动态库和静态库(Linux环境)
  • TOGAF之架构标准规范-信息系统架构 | 应用架构
  • Leetcode2595:奇偶位数
  • 纯手工搭建整套CI/CD流水线指南
  • 基于深度学习进行呼吸音检测的详细示例
  • 物联网简介集合
  • 数电笔记——第一章 数制和码制
  • 基于 Flask 与 MySQL 构建简单的博客系统
  • Java并发编程——并发容器
  • 【Research Proposal】基于提示词方法的智能体工具调用研究——研究现状
  • 动态库加载(可执行程序和地址空间)
  • 用LightRAG+智谱GLM-4提升政务对话精度:从知识图谱到精准问答的实战指南
  • 25重庆事业单位联考明日报名[特殊字符]全流程[特殊字符]
  • Dev-Atlas:典型发育青少年功能性脑网络参考图谱
  • RedissonClient:ZSet(有序集合)上手教程
  • 九、OSG学习笔记-NodeVisitor节点遍历器
  • 当滑动组件连续触发回调函数的三种解决办法
  • 回调处理器
  • Qt程序退出相关资源释放问题
  • MySQL基础回顾#1
  • jQuery UI CSS 框架 API
  • PyTorch 系统教程:PyTorch 入门项目(简单线性回归)
  • 使用代码与 AnythingLLM 交互的基本方法和示例
  • 30天开发操作系统 第22天 -- 用C语言编写应用程序
  • 模型训练与优化遇到的问题3:安装STM32Cube.AI
  • Webpack的持久化缓存机制具体是如何实现的?