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:~$
相关推荐
不会敲代码14 小时前
手写 Mini React:从 JSX 到虚拟 DOM 再到 render,搞懂 React 底层原理
前端·javascript·react.js
kyriewen5 小时前
你的代码仓库变成“毛线团”了?Monorepo 用 Turborepo 拆成“乐高积木”
前端·javascript·面试
身如柳絮随风扬5 小时前
你知道什么是 Ajax 吗?—— 从入门到原理,一篇彻底搞懂
前端·ajax·okhttp
旷世奇才李先生6 小时前
Vue3\+TypeScript 2026实战——企业级前端项目架构搭建与性能优化全指南
前端·架构·typescript
Beginner x_u6 小时前
前端八股整理(工程化 02)|CommonJS/ESM、Webpack Loader/Plugin 与Vite 对比
前端·webpack·node.js·plugin·loader
openKaka_7 小时前
createRoot 到底创建了什么:FiberRootNode 和 HostRootFiber 的初始化过程
前端·javascript·react.js
习明然7 小时前
UniApp开发体验感受总结
前端·uni-app
刀法如飞8 小时前
Claude Code Skills 推荐:2026年最值得安装的10个AI技能
前端·后端·ai编程
阿豪只会阿巴8 小时前
【没事学点啥】TurboBlog轻量级个人博客项目——项目介绍
javascript·python·django·html
Lee川8 小时前
面试手写 KeepAlive:React 组件缓存的实现原理
前端·react.js·面试