AI大模型-硅基流动功能示例
包括:聊天、文生图等。
·
硅基流动:致力于为开发者提供更快、更全面、体验更丝滑的模型 API,助力开发者和企业聚焦产品创新,无须担心产品大规模推广所带来的高昂算力成本。提供免费的模型以及付费模型,按需使用。
注册获取apiKey:https://cloud.siliconflow.cn/account/ak
代码示例:(包括:文本对话、文生图、向量化、重排序等功能)
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()
# os.environ['SILICON_FLOW_API_KEY'] = 'your sk-xxxxxxxxxxxxxxx'
def chat_openai(prompt):
url = "https://api.siliconflow.cn/v1/chat/completions"
payload = {
"messages": [
{
"content": prompt,
"role": "user"
}
],
# THUDM/glm-4-9b-chat, deepseek-ai/DeepSeek-R1-Distill-Llama-8B
"model": "THUDM/glm-4-9b-chat",
"frequency_penalty": 0.5,
"max_tokens": 509,
"n": 0,
"response_format": {"type": "text"},
"temperature": 0.7,
"top_k": 50,
"top_p": 0.7,
# "tools": [
# {
# "type": "function",
# "function": {
# "description": "<string>",
# "name": "<string>",
# "parameters": {},
# "strict": False
# }
# }
# ]
}
headers = {
"Authorization": "Bearer " + os.getenv('SILICON_FLOW_API_KEY'),
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
res = json.loads(response.text).get('choices')[0].get('message').get('content')
print(res)
return res
def text_to_image(text):
url = "https://api.siliconflow.cn/v1/images/generations"
payload = {
"batch_size": 1, # 输出图片个数 1-4
"guidance_scale": 11, # 图文匹配程度 0-20,低则创造性,高则更匹配
"image_size": "576x1024", # 1024x1024, 512x1024, 768x512, 768x1024, 1024x576, 576x1024
"model": "black-forest-labs/FLUX.1-schnell", # 模型名称
"num_inference_steps": 40, # 推理步骤数 1-50
"prompt": text,
# "seed": 123, # 随机种子,seed+prompt一致时,下一次则可能生成一样的图
"prompt_enhancement": True # 开启将提示优化为详细的、模型友好的版本
}
headers = {
"Authorization": "Bearer " + os.getenv('SILICON_FLOW_API_KEY'),
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
text = response.text
print(text)
text = text.replace('\\u0026', '&') # 替换 \u0026 为 &
print(text)
res = json.loads(text).get('data')[0].get('url')
return res
import base64
def image_to_base64(image_path):
with open(image_path, 'rb') as image_file:
image_data = image_file.read()
base64_data = base64.b64encode(image_data)
return base64_data.decode('utf-8')
def image_to_image(image_path, prompt):
url = "https://api.siliconflow.cn/v1/images/generations"
payload = {
"batch_size": 1,
"guidance_scale": 9,
"image_size": "1024x1024",
# stabilityai/stable-diffusion-2-1.0, stabilityai/stable-diffusion-xl-base-1.0
"model": "stabilityai/stable-diffusion-xl-base-1.0",
"num_inference_steps": 39,
"prompt": prompt,
"image": image_to_base64(image_path),
}
headers = {
"Authorization": "Bearer " + os.getenv('SILICON_FLOW_API_KEY'),
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
text = response.text
print(text)
text = text.replace('\\u0026', '&') # 替换 \u0026 为 &
print(text)
def embed_text(text):
url = "https://api.siliconflow.cn/v1/embeddings"
payload = {
"model": "BAAI/bge-m3",
"input": text,
"encoding_format": "float"
}
headers = {
"Authorization": "Bearer " + os.getenv('SILICON_FLOW_API_KEY'),
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)
def rerank(query, docs):
url = "https://api.siliconflow.cn/v1/rerank"
payload = {
"model": "BAAI/bge-reranker-v2-m3",
"query": query,
"documents": docs,
"top_n": 4,
"return_documents": False,
"max_chunks_per_doc": 1024,
"overlap_tokens": 80
}
headers = {
"Authorization": "Bearer " + os.getenv('SILICON_FLOW_API_KEY'),
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)
# rerank('Apple', ["苹果", "香蕉", "水果", "蔬菜"])
# image_to_image('./doc/2023.jpg', "背景改成白色")
res = chat_openai("""我打算生成一张图片,内容大概是:\n
夕阳下奔跑的少年
\n 帮我写一段细节更详细一点的提示语,包括高清、高品质、细节精致、真实写真风格等等。""")
text_to_image(res)
更多推荐


所有评论(0)