2 Star 16 Fork 1

Gitee 极速下载/screenshot-to-code-llm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/abi/screenshot-to-code
克隆/下载
openai_client.py 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
Abi Raja 提交于 2025-07-28 04:51 +08:00 . bump output max tokens for GPT 4.1
import time
from typing import Awaitable, Callable, List
from openai import AsyncOpenAI
from openai.types.chat import ChatCompletionMessageParam, ChatCompletionChunk
from llm import Completion
async def stream_openai_response(
messages: List[ChatCompletionMessageParam],
api_key: str,
base_url: str | None,
callback: Callable[[str], Awaitable[None]],
model_name: str,
) -> Completion:
start_time = time.time()
client = AsyncOpenAI(api_key=api_key, base_url=base_url)
# Base parameters
params = {
"model": model_name,
"messages": messages,
"timeout": 600,
}
# O1 doesn't support streaming or temperature
if model_name not in ["o1-2024-12-17", "o4-mini-2025-04-16", "o3-2025-04-16"]:
params["temperature"] = 0
params["stream"] = True
# 4.1 series
if model_name in [
"gpt-4.1-2025-04-14",
"gpt-4.1-mini-2025-04-14",
"gpt-4.1-nano-2025-04-14",
]:
params["temperature"] = 0
params["stream"] = True
params["max_tokens"] = 20000
if model_name == "gpt-4o-2024-05-13":
params["max_tokens"] = 4096
if model_name == "gpt-4o-2024-11-20":
params["max_tokens"] = 16384
if model_name == "o1-2024-12-17":
params["max_completion_tokens"] = 20000
if model_name in ["o4-mini-2025-04-16", "o3-2025-04-16"]:
params["max_completion_tokens"] = 20000
params["stream"] = True
params["reasoning_effort"] = "high"
# O1 doesn't support streaming
if model_name == "o1-2024-12-17":
response = await client.chat.completions.create(**params) # type: ignore
full_response = response.choices[0].message.content # type: ignore
else:
stream = await client.chat.completions.create(**params) # type: ignore
full_response = ""
async for chunk in stream: # type: ignore
assert isinstance(chunk, ChatCompletionChunk)
if (
chunk.choices
and len(chunk.choices) > 0
and chunk.choices[0].delta
and chunk.choices[0].delta.content
):
content = chunk.choices[0].delta.content or ""
full_response += content
await callback(content)
await client.close()
completion_time = time.time() - start_time
return {"duration": completion_time, "code": full_response}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mirrors/screenshot-to-code-llm.git
git@gitee.com:mirrors/screenshot-to-code-llm.git
mirrors
screenshot-to-code-llm
screenshot-to-code-llm
main

搜索帮助