大模型开发实战篇5:多模态-文生图模型API

大模型向量数据库云通信

大模型文生图是一种基于人工智能大模型的技术,能够将自然语言文本描述转化为对应的图像。目前非常火的AI大模型赛道,有很多公司在此赛道竞争。详情可看这篇文章《AI大模型文生图功能初识和体验,真的太惊艳了》

今天我们来看下如何调用WebAPI来实现文生图功能。我们一般都会将OpenAI的接口,因为OpenAI是标杆,其他大模型都以它为参考,并且很多大模型的接口都复刻了OpenAI的接口,不管是接口形式还是参数定义基本都一样;也就是只要学会了OpenAI的接口,很多其他大模型也就会调用了。

picture.image

一、OpenAI的文生图模型 DALL·E

OpenAI Images API 提供了三种与图像交互的方法:

  1. 基于文本提示生成图像(DALL·E 3 和 DALL·E 2)
  2. 通过模型编辑(替换)已存在图像的某些区域,根据新的文本提示创建编辑过的图像版本(仅限 DALL·E 2)
  3. 创建现有图像的变体(仅限 DALL·E 2)

本文主要介绍第一种文生图像的使用方法。

关于 DALL·E 3 模型更新的更多内容,请参考 OpenAI Cookbook的官方链接:

https://cookbook.openai.com/articles/what\_is\_new\_with\_dalle\_3

picture.image

图像生成 API参数

  • prompt:(必须传)提示词,一段对所需图像的文字描述。对于 DALL・E-2,最大长度为 1000 个字符;对于 DALL・E-3,最大长度为 4000 个字符。
  • model('dall-e-2' 或 'dall-e-3'):您正在使用的模型。请注意将其设置为 'dall-e-3',因为如果为空,默认为 'dall-e-2'。
  • style('natural' 或 'vivid'):生成图像的风格。必须是 'vivid' 或 'natural' 之一。'vivid' 会使模型倾向于生成超现实和戏剧性的图像。'natural' 会使模型产生更自然、不那么超现实的图像。默认为 'vivid'。
  • quality('standard' 或 'hd'):将生成的图像质量。'hd' 创建细节更精细、整体一致性更高的图像。默认为 'standard'。
  • n(int):要生成的图像数量。必须在1到10之间。默认为1。对于 dall-e-3,只支持 n=1。
  • size(...):生成图像的尺寸。对于 DALL·E-2 模型,必须是 256x256、512x512 或 1024x1024 之一。对于 DALL·E-3 模型,必须是 1024x1024、1792x1024 或 1024x1792 之一。
  • response_format('url' 或 'b64_json'):返回生成图像的格式。必须是 "url" 或 "b64_json" 之一。默认为 "url"。url形式的图片地址是有效期限制的,一般是2个小时后过期,所以在代码开发中若需要长期保留图片,需要将图片及时保存到本地。
  • user(str):代表您的终端用户的唯一标识符,将帮助 OpenAI 监控和检测滥用。

了解更多可取官方api教程:https://platform.openai.com/docs/api-reference/images/create

代码演示

1、标准模型(quality="standard")

  
from openai import OpenAI  
client = OpenAI(api_key="sk-xxx", base_url="https://vip.apiyi.com/v1")  
response = client.images.generate(  
    model="dall-e-3",  
    prompt="In the style of a Polaroid photo, a cute Japanese high school girl, dressed in her school uniform, with short black hair, is smiling and posing at the entrance of her high school. The soft, warm tones of the Polaroid film capture the gentle morning light, highlighting the neat pleats of her skirt and the crisp white collar of her blouse. Her cheerful expression and bright eyes reflect the excitement of a new day, and the background shows the familiar brick walls and arched entryway of the school, with a few cherry blossom petals scattered on the ground, adding a touch of seasonal beauty.",  
    size="1024x1024",  
    quality="standard",  
    n=1,  
)  
image_url = response.data[0].url  
print(image_url)

可以看到,模型为dall-e-3, quality的参数值为standard标准版本,尺寸1024*1024 。

返回结果:

  
https://dalleprodsec.blob.core.windows.net/private/images/533473e0-f83d-4cde-980f-c392386ed3c9/generated\_00.png?se=2025-02-17T14%3A09%3A29Z&sig=%2FoN77%2Bkb%2FKA4OY84bNaQ7N%2BZ1ZSVC2js1%2Ff9UoD6W4s%3D&ske=2025-02-22T15%3A19%3A26Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2025-02-15T15%3A19%3A26Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02

picture.image

2、高清模式(quality="hd")

  
response = client.images.generate(  
    model="dall-e-3",  
    prompt="In the style of a Polaroid photo, a cute Japanese high school girl, dressed in her school uniform, with short black hair, is smiling and posing at the entrance of her high school. The soft, warm tones of the Polaroid film capture the gentle morning light, highlighting the neat pleats of her skirt and the crisp white collar of her blouse. Her cheerful expression and bright eyes reflect the excitement of a new day, and the background shows the familiar brick walls and arched entryway of the school, with a few cherry blossom petals scattered on the ground, adding a touch of seasonal beauty.",  
    size="1024x1024",  
    quality="hd",  
    n=1,  
)  
print(response.data[0].url)

3、自然风格(style="natural")

  
response = client.images.generate(  
    model="dall-e-3",  
    prompt="In the style of a Polaroid photo, a cute Japanese high school girl, dressed in her school uniform, with short black hair, is smiling and posing at the entrance of her high school. The soft, warm tones of the Polaroid film capture the gentle morning light, highlighting the neat pleats of her skirt and the crisp white collar of her blouse. Her cheerful expression and bright eyes reflect the excitement of a new day, and the background shows the familiar brick walls and arched entryway of the school, with a few cherry blossom petals scattered on the ground, adding a touch of seasonal beauty.",  
    size="1024x1024",  
    quality="standard",  
    n=1,  
    style="natural"  
)  
print(response.data[0].url)

picture.image

4、戏剧风格(style="vivid")

  
response = client.images.generate(  
    model="dall-e-3",  
    prompt="a white siamese cat",  
    size="1024x1024",  
    quality="standard",  
    n=1,  
    style="vivid"  
)  
print(response.data[0].url)

picture.image

二、智普图像生成模型

cogview-4适用于图像生成任务,通过对用户文字描述快速、精准的理解,让AI的图像表达更加精确和个性化。

请求参数

| 参数名 | 类型 | 必填 | 描述 | | --- | --- | --- | --- | | model | String | 是 | 模型编码 | | prompt | String | 是 | 所需图像的文本描述 | | size | String | 否 | 图片尺寸,可选值:1024x1024,768x1344,864x1152,1344x768,1152x864,1440x720,720x1440,默认是1024x1024。 | | user_id | String | 否 | 终端用户的唯一ID,协助平台对终端用户的违规行为、生成违法及不良信息或其他滥用行为进行干预。ID长度要求:最少6个字符,最多128个字符。 |

响应参数

| 参数名称 | 类型 | 参数说明 | | --- | --- | --- | | created | String | 请求创建时间,是以秒为单位的Unix时间戳。 | | data | List | 数组,包含生成的图片 URL。目前数组中只包含一张图片。 | |  url | String | 图片链接。图片的临时链接有效期为 30天,请及时转存图片。 | | content_filter | List | 返回内容安全的相关信息。 | |  role | String | 安全生效环节,包括 role = assistant 模型推理,role = user 用户输入,role = history 历史上下文 | |  level | Integer | 严重程度 level 0-3,level 0表示最严重,3表示轻微 |

调用示例

  
from zhipuai import ZhipuAI  
client = ZhipuAI(api_key="") # 请填写您自己的APIKey  
response = client.images.generations(  
    model="cogview-4", #填写需要调用的模型编码  
    prompt="一只可爱的小猫咪",  
)  
print(response.data[0].url)

响应示例

  
{  
  "created": 1703485556,  
  "data": [  
      {  
          "url": "https://......"  
      }  
  ]  
}
0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
大模型解决方案白皮书:社交陪伴场景全流程落地指南
随着大模型技术持续突破,AI正加速重塑社交娱乐的形态与体验。其中,陪伴式聊天因用户黏性强、互动频次高,成为大模型商业化落地的关键赛道。随着模型能力跃升至万亿参数级,AI从工具属性正迈向情感交互生态,现象级产品的诞生条件逐渐成熟。 本白皮书聚焦AI陪伴聊天应用开发,面向“从何起步、如何落地”的新手困惑,系统拆解从需求定义到产品上线的关键流程。我们结合工程化实践路径,打造模块化知识体系与渐进式开发框架,帮助开发者在30天内完成从技术认知到产品原型的跃升,快速构建具备基础交互能力的Web或App应用,迈出大模型
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论