三种国产大语言模型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"))    
相关推荐
古一木6 分钟前
opencv 形态学操作
人工智能·opencv·计算机视觉
KK不Battle13 分钟前
AI 配图缺的不是模型,是工程:开源 skill social-illustrator 上手教程
人工智能
2601_9637491033 分钟前
越华环保集团污水监测云边协同架构:数字化污水治理Modbus-MQTT链路实现方案
人工智能·架构
u01030552734 分钟前
Java实用类应用精讲
人工智能·1024程序员节
城事漫游Molly37 分钟前
用AI评估文献可靠性——相关性、可靠性、时效性三维框架
人工智能·ai for science·文献综述·文献阅读·科研方法·博士生必读·文献评估
聚铭网络1 小时前
狂飙的AI,失控的安全……
人工智能·安全
imbackneverdie1 小时前
写文献综述,时间脉络和主题分类到底怎么选?
人工智能·ai·aigc·论文·科研·ai写作·医学
难以怀瑾1 小时前
pytest.param`与 allure.dynamic.title()全解析
python
weixin_446260851 小时前
生成式与智能体AI认知能力缺陷分类体系
人工智能·分类·数据挖掘
网渡科技1 小时前
GEO搜索优化:大模型引用率提升的六大实战策略
人工智能·搜索引擎·chatgpt