g4f提供的模型调用:python JavaScript和curl

g4f提供模型的使用,例子页面:G4F - Providers and Models

可以这样:

python

python 复制代码
from g4f.client import Client

client = Client()
response = client.chat.completions.create(
    model="",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)

例如:

python 复制代码
(.venv) G:\traework\airoot\kotti-py312>python
Python 3.12.9 (tags/v3.12.9:fdb8142, Feb  4 2025, 15:27:58) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from g4f.client import Client
>>>
>>> client = Client()
>>> response = client.chat.completions.create(
...     model="",
...     messages=[{"role": "user", "content": "Hello!"}],
... )
>>> print(response.choices[0].message.content)
Hey there! How's your day going?

JavaScript

复制代码
import { createClient } from 'https://g4f.dev/dist/js/providers.js';

const client = createClient('default');
const result = await client.chat.completions.create({
    model: 'auto',
    messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(result.choices[0].message.content);

就这个没有测试成功....

curl

bash 复制代码
curl https://g4f.space/api/auto/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "auto",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

例如:

python 复制代码
skywalk@ubjail1:~$ curl https://g4f.space/api/auto/chat/completions \
>   -H "Content-Type: application/json" \
>   -d '{
>     "model": "auto",
>     "messages": [{"role": "user", "content": "Hello!"}]
>   }'
{"id":"chatcmpl-1776434719606","object":"chat.completion","created":1776434719,"model":"glm-5","choices":[{"index":0,"message":{"role":"assistant","content":"Hello! How can I help you today?","reasoning_content":"The user has sent a simple greeting: \"Hello!\".\nMy goal is to respond politely and helpfully to initiate a conversation.\n\n1.  **Analyze the input**: It's a standard English greeting.\n2.  **Determine the intent**: The user is starting an interaction. No complex task is requested yet.\n3.  **Formulate a response**:\n    *   Acknowledge the greeting.\n    *   Offer assistance.\n    *   Keep it friendly and concise.\n4.  **Drafting options**:\n    *   Option A: \"Hi there! How can I help you?\" (Simple, effective)\n    *   Option B: \"Hello! I am an AI assistant. What can I do for you today?\" (A bit more formal)\n    *   Option C: \"Hey! Nice to meet you. Do you have any questions?\" (Casual)\n5.  **Selecting the best fit**: Option A is a good balance of friendly and professional.\n6.  **Final Polish**: \"Hello! How can I help you today?\" is a classic, reliable response.\n\n*Self-Correction during thought process*: I should ensure I don't sound too robotic, but also maintain professional boundaries. A simple, warm greeting is best.\n\n**Final Output Generation**: \"Hello! How can I help you today?\""},"finish_reason":"stop"}],"usage":{"prompt_tokens":7,"completion_tokens":284,"total_tokens":291}}skywalk@ubjail1:~$
相关推荐
郭老二9 小时前
【前端】vue3:编译步骤
前端
不在逃避q10 小时前
Trae/Vs Code/Cursor命令行无法跑npm命令
前端·arcgis·npm
夏殇之殁10 小时前
包中创建自定义列表项。 . 使用自定义列表项进行数据绑定 . 将天气预报数据保存到本地内存表,通过LiveBindings进行显示。 ...
服务器·前端·javascript
The Chosen One98511 小时前
高进度算法模板速记(待完善)
java·前端·算法
陈随易11 小时前
MoonBit抓包模块pcap,查看电脑的每一次联网通信
前端·后端·程序员
新中地GIS开发老师12 小时前
WebGIS开发学生作品|低空航天管理与航线规划系统
前端·javascript·webgis·三维gis开发
用户0595401744612 小时前
大模型对话记忆持久化踩坑实录:用 Playwright 自动化测了 300 次,终于揪出会话丢失的真凶
前端·css
丙氨酸長鏈12 小时前
Web前端入门第 问:JavaScript 一个简单的 IndexedDB 数据库入门示例
前端·javascript·数据库
kyriewen13 小时前
面试官让我手写虚拟列表——AI生成的版本,快速滚动几下就白屏了
前端·javascript·面试
IT_陈寒13 小时前
Vite热更新失效?你可能漏了这个配置
前端·人工智能·后端