三种国产大语言模型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"))    
相关推荐
加多7 小时前
DeepSeek Harness 深度分析:用途、问题、架构原理与使用指南
人工智能·架构
测试19987 小时前
Python接口测试之requests库安装和导入
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
%477 小时前
DAY 34
python
风流 少年7 小时前
Spring AI 2.0:Flux
java·人工智能·spring
小马过河R7 小时前
Graph Engineering 深度解析:模型越强,越需要给它画好“地图”
人工智能·langchain·graph·ai工程化·harness·驾驭工程
leisoo80977 小时前
涨停板次日表现因子怎么挖掘本地化Python全流程实战
大数据·人工智能·python
像颗糖7 小时前
AG-UI:把 Agent 与前端之间的“私有暗号”变成标准协议
python·agent·ai编程
lucas_AI7 小时前
喂张白纸也能吐出证件号?文档 MLLM 的"关系级泄露"被测出来了
人工智能·算法·掘金技术征文
Zane19947 小时前
类也是对象?一文讲透元类 metaclass 这件"深度魔法"
后端·python
Old Uncle Tom7 小时前
手机银行用户画像设计
人工智能·智能手机