三种国产大语言模型Python免费调用

基础三大模型,需要先去官方注册获得key;后续可以使用下列代码调用

1.腾讯

安装:

复制代码
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

实例:

复制代码
import jsonimport typesfrom tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.hunyuan.v20230901 import hunyuan_client, modelstry:    cred = credential.Credential("你的SecretId", "你的SecretKey")    httpProfile = HttpProfile()    httpProfile.endpoint = "hunyuan.tencentcloudapi.com"    clientProfile = ClientProfile()    clientProfile.httpProfile = httpProfile    req = models.ChatCompletionsRequest()    params = {        "Model": "hunyuan-lite",        "Messages": [            {                "Role": "user",                "Content": "你好"            }        ]    }    req.from_json_string(json.dumps(params))    resp = client.ChatCompletions(req)    if isinstance(resp, types.GeneratorType):         for event in resp:            print(event)    else:         print(resp)except TencentCloudSDKException as err:    print(err)

2.讯飞

安装:

复制代码
pip install spark-ai-python

实例:

复制代码
from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandlerfrom sparkai.core.messages import ChatMessage
def get_response(text):    if text is not None:        SPARKAI_URL = 'wss://spark-api.xf-yun.com/v1.1/chat'        SPARKAI_APP_ID = ''        SPARKAI_API_SECRET = ''        SPARKAI_API_KEY = ''        SPARKAI_DOMAIN = 'general'        spark = ChatSparkLLM(spark_api_url=SPARKAI_URL, spark_app_id=SPARKAI_APP_ID, spark_api_key=SPARKAI_API_KEY,                              spark_api_secret=SPARKAI_API_SECRET, spark_llm_domain=SPARKAI_DOMAIN, streaming=False)        messages = [ChatMessage(role="user", content=text)]        handler = ChunkPrintHandler()        st = spark.generate([messages], callbacks=[handler])        return st
response = get_response("你好")print(response)

3.百度:

安装:

复制代码
pip install requests

import requestsimport jsondef get_access_token():    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=&client_secret="    payload = json.dumps("")    headers = {        'Content-Type': 'application/json',        'Accept': 'application/json'    }    response = requests.request("POST", url, headers=headers, data=payload)    return response.json().get("access_token")

def get_response(text):       url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=" + get_access_token()    payload = json.dumps({"messages": [{"role": "user","content": "hi"}]})    headers = {        'Content-Type': 'application/json'    }    response = requests.request("POST", url, headers=headers, data=payload)        print(response.json().get("result"))    
相关推荐
知识分享小能手8 小时前
深度学习学习教程,从入门到精通,数值计算 — 知识点详解(4)
人工智能·深度学习·学习
三声三视8 小时前
审计清单函数名写成 def?tri-checklist 的 diff 解析在 Python 改名场景下悄悄翻车
人工智能·ai·skillhub·tri-checklist·tri-skills
嘟嘟嘟95278 小时前
Kubernetes 引入 KYAML:更安全的 YAML 子集
人工智能·架构·开源
智购科技自动售货机厂家8 小时前
2026自动售货机设备清洁效果自动验证:从图像比对到评分算法的工程实践~YH
人工智能·算法·计算机视觉
财迅通Ai8 小时前
光智科技全景透视:一家被“光学元件”标签遮蔽的稀散金属材料平台
大数据·人工智能·科技·光智科技
小刘在重生~8 小时前
Java常用类|String类详解 + BigDecimal精准计算(含面试题)
java·开发语言·python
AI技术新视界9 小时前
失控的认知外包:大语言模型如何像病毒般入侵人类思维与社会生态
人工智能·llm·认知科学
后端小肥肠9 小时前
还在找PPT 生成工具?我集成了 GitHub 高星 Skill,自动匹配最优方案
人工智能·aigc·agent
打破砂锅问到底0079 小时前
115 行把 Qwen3-8B 跑进浏览器:WebLLM 本地推理实战
人工智能·ai·llm
二川bro9 小时前
幻觉≠提示词注入!拆解GPT‑6 Astra三层防御架构的致命短板
人工智能