赞
踩
本篇文章是针对星火大模型api接口使用的新篇章,本次主要是介绍对于pythonSDK使用,以及图片理解等新功能。相对于上篇博客中的使用方法,本次的教程相对来说更简单方便。话不多说,直接享用。
参考上篇文章:
这个图片理解的功能也是需要申请key,流程一样,同时也有200万免费token
项目仅支持 Python3.8+
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple spark_ai_python
如果清华源版本不可用,请使用一下命令升级到最新版本:
pip install -i https://repo.model.xfyun.cn/api/packages/administrator/pypi/simple spark_ai_python --upgrade
- # coding: utf-8
-
- import os
- from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandler
- from sparkai.core.messages import ChatMessage
- try:
- from dotenv import load_dotenv
- except ImportError:
- raise RuntimeError('Python environment for SPARK AI is not completely set up: required package "python-dotenv" is missing.') from None
-
- load_dotenv()
-
- if __name__ == '__main__':
- appid = "" # 填写控制台中获取的 APPID 信息
- api_secret = "" # 填写控制台中获取的 APISecret 信息
- api_key = "" # 填写控制台中获取的 APIKey 信息
- spark = ChatSparkLLM(
- spark_api_url="wss://spark-api.xf-yun.com/v3.5/chat",
- spark_app_id=appid,
- spark_api_key=api_key,
- spark_api_secret=api_secret,
- spark_llm_domain="generalv3.5",
- streaming=False,
- )
- messages = [ChatMessage(
- role="user",
- content='你好呀'
- )]
- a = spark.generate([messages])
- for response in a.generations:
- print(response[0].text)
-
注意当streaming
设置为 False
的时候, callbacks 并不起作用。
这里要说一下历史对话问题:
如上图,数组中传入格式,最大索引为本次最新问题,其他为历史对话
- import os
-
- from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandler,AsyncChunkPrintHandler
- from sparkai.core.messages import ChatMessage
-
- # spark 授权信息
- SPARKAI_APP_ID=''
- SPARKAI_API_KEY=''
- SPARKAI_API_SECRET=''
-
-
- def test_starcoder(text):
- """
- 不带历史的单输入
- :param text:
- :return:
- """
- messages = [{'role': 'user',
- 'content': text}]
- spark = ChatSparkLLM(
- spark_api_url="wss://spark-api.xf-yun.com/v3.5/chat",
- spark_app_id=SPARKAI_APP_ID,
- spark_api_key=SPARKAI_API_KEY,
- spark_api_secret=SPARKAI_API_SECRET,
- spark_llm_domain="generalv3.5",
- streaming=True,
- max_tokens= 1024,
- )
- messages = [
- ChatMessage(
- role="user",
- content=messages[0]['content']
-
- )]
-
- a = spark.stream(messages)
- for message in a:
- print(message.content)
-
-
- if __name__ == '__main__':
- t = '今天世界和平吗'
- test_starcoder(t)
- # spark 授权信息
- SPARKAI_APP_ID=''
- SPARKAI_API_KEY=''
- SPARKAI_API_SECRET=''
- async def test_astream():
- from sparkai.log.logger import logger
- #logger.setLevel("debug")
- from sparkai.core.callbacks import StdOutCallbackHandler
- messages = [{'role': 'user',
- 'content': "帮我生成一段代码,爬取baidu.com"}]
- spark = ChatSparkLLM(
- spark_api_url="wss://xingchen-api.cn-huabei-1.xf-yun.com/v1.1/chat",
- spark_app_id=SPARKAI_APP_ID,
- spark_api_key=SPARKAI_API_KEY,
- spark_api_secret=SPARKAI_API_SECRET,
- spark_llm_domain="xsstarcoder27binst",
- streaming=True,
- max_tokens= 1024,
- )
- messages = [
- ChatMessage(
- role="user",
- content=messages[0]['content']
-
- )]
- handler = AsyncChunkPrintHandler()
- a = spark.astream(messages, config={"callbacks": [handler]})
- async for message in a:
- print(message)
-
- if __name__ == '__main__':
- import asyncio
- asyncio.run(test_astream())
- import base64
- import os
-
- from sparkai.core.messages import ImageChatMessage
- from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandler
-
- SPARKAI_APP_ID=''
- SPARKAI_API_KEY=''
- SPARKAI_API_SECRET=''
-
- image_content = base64.b64encode(open("屏幕截图 2024-05-20 101725.png",'rb').read())
-
- spark = ChatSparkLLM(
- # spark_api_url="wss://spark-api.xf-yun.com/v3.5/chat",
- spark_app_id=SPARKAI_APP_ID,
- spark_api_key=SPARKAI_API_KEY,
- spark_api_secret=SPARKAI_API_SECRET,
- spark_llm_domain="image",
- streaming=False,
- user_agent="test"
-
- )
- messages = [ImageChatMessage(
- role="user",
- content=image_content,
- content_type="image"
- ),ImageChatMessage(
- role="user",
- content="这是什么图",
- content_type="text"
- )]
- handler = ChunkPrintHandler()
- a = spark.generate([messages], callbacks=[])
- print(a)
注意:这个key不能用3.5大模型的,这个是独立的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。