Skip to content

Commit

Permalink
Stepfun (#54)
Browse files Browse the repository at this point in the history
* Add StepFunChat and StepFunSettings classes
* Update version and add new model

---------

Co-authored-by: wangyuxin <[email protected]>
Co-authored-by: wangyuxin <[email protected]>
  • Loading branch information
3 people authored Mar 26, 2024
1 parent cf78931 commit 452cb46
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ Generate 允许用户通过统一的 api 访问多平台的生成式模型,当
| 灵积 DashScope ||||||
| 百川智能 Baichuan ||||||
| Minimax ||||||
| 混元 Hunyuan ||||| |
| 混元 Hunyuan ||||| |
| 智谱 Zhipu ||||||
| 月之暗面 Moonshot||||||
| DeepSeek ||||||
| 零一万物 Yi ||||||
| 零一万物 Yi ||||||
| 阶跃星辰 StepFun ||||||

## Features

Expand Down
6 changes: 6 additions & 0 deletions generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
OpenAIChatParameters,
Prompt,
RemoteChatCompletionModel,
StepFunChat,
StepFunChatParameters,
WenxinChat,
WenxinChatParameters,
YiChat,
Expand Down Expand Up @@ -73,6 +75,7 @@
MoonshotSettings,
OpenAISettings,
QianfanSettings,
StepFunSettings,
YiSettings,
ZhipuSettings,
)
Expand Down Expand Up @@ -105,6 +108,9 @@
'ZhipuChatParameters',
'ZhipuCharacterChat',
'ZhipuCharacterChatParameters',
'StepFunChat',
'StepFunChatParameters',
'StepFunSettings',
'WenxinChat',
'WenxinChatParameters',
'HunyuanChat',
Expand Down
5 changes: 5 additions & 0 deletions generate/chat_completion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
MoonshotChatParameters,
OpenAIChat,
OpenAIChatParameters,
StepFunChat,
StepFunChatParameters,
WenxinChat,
WenxinChatParameters,
YiChat,
Expand Down Expand Up @@ -70,6 +72,7 @@
(DashScopeMultiModalChat, DashScopeMultiModalChatParameters),
(MoonshotChat, MoonshotChatParameters),
(DeepSeekChat, DashScopeChatParameters),
(StepFunChat, StepFunChatParameters),
(YiChat, YiChatParameters),
]

Expand Down Expand Up @@ -107,6 +110,8 @@
'BailianChatParameters',
'YiChat',
'YiChatParameters',
'StepFunChat',
'StepFunChatParameters',
'AnthropicChat',
'AnthropicChatParameters',
'DashScopeChat',
Expand Down
3 changes: 3 additions & 0 deletions generate/chat_completion/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from generate.chat_completion.models.minimax_pro import MinimaxProChat, MinimaxProChatParameters
from generate.chat_completion.models.moonshot import MoonshotChat, MoonshotChatParameters
from generate.chat_completion.models.openai import OpenAIChat, OpenAIChatParameters
from generate.chat_completion.models.stepfun import StepFunChat, StepFunChatParameters
from generate.chat_completion.models.wenxin import WenxinChat, WenxinChatParameters
from generate.chat_completion.models.yi import YiChat, YiChatParameters
from generate.chat_completion.models.zhipu import (
Expand Down Expand Up @@ -49,6 +50,8 @@
'OpenAIChatParameters',
'WenxinChat',
'WenxinChatParameters',
'StepFunChat',
'StepFunChatParameters',
'YiChat',
'YiChatParameters',
'ZhipuChat',
Expand Down
72 changes: 72 additions & 0 deletions generate/chat_completion/models/stepfun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from __future__ import annotations

from typing import AsyncIterator, ClassVar, Iterator, List, Optional

from pydantic import Field, PositiveInt
from typing_extensions import Annotated, Unpack, override

from generate.chat_completion.message import Prompt
from generate.chat_completion.model_output import ChatCompletionOutput, ChatCompletionStreamOutput
from generate.chat_completion.models.openai_like import OpenAILikeChat
from generate.http import HttpClient
from generate.model import ModelParameters, RemoteModelParametersDict
from generate.platforms import StepFunSettings
from generate.types import Probability

Temperature = Annotated[float, Field(ge=0, le=2)]


class StepFunChatParameters(ModelParameters):
temperature: Optional[Temperature] = None
top_p: Optional[Probability] = None
max_tokens: Optional[PositiveInt] = None
presence_penalty: Optional[Annotated[float, Field(ge=-2, le=2)]] = None
frequency_penalty: Optional[Annotated[float, Field(ge=-2, le=2)]] = None


class StepFunParametersDict(RemoteModelParametersDict, total=False):
temperature: Optional[Temperature]
top_p: Optional[Probability]
max_tokens: Optional[PositiveInt]
presence_penalty: Optional[float]
frequency_penalty: Optional[float]


class StepFunChat(OpenAILikeChat):
model_type: ClassVar[str] = 'stepfun'
available_models: ClassVar[List[str]] = ['step-1-32k', 'step-1v-32k', 'step-1-200k']

parameters: StepFunChatParameters
settings: StepFunSettings

def __init__(
self,
model: str = 'step-1-32k',
parameters: StepFunChatParameters | None = None,
settings: StepFunSettings | None = None,
http_client: HttpClient | None = None,
) -> None:
parameters = parameters or StepFunChatParameters()
settings = settings or StepFunSettings() # type: ignore
http_client = http_client or HttpClient()
model = model
super().__init__(model=model, parameters=parameters, settings=settings, http_client=http_client)

@override
def generate(self, prompt: Prompt, **kwargs: Unpack[StepFunParametersDict]) -> ChatCompletionOutput:
return super().generate(prompt, **kwargs)

@override
async def async_generate(self, prompt: Prompt, **kwargs: Unpack[StepFunParametersDict]) -> ChatCompletionOutput:
return await super().async_generate(prompt, **kwargs)

@override
def stream_generate(self, prompt: Prompt, **kwargs: Unpack[StepFunParametersDict]) -> Iterator[ChatCompletionStreamOutput]:
yield from super().stream_generate(prompt, **kwargs)

@override
async def async_stream_generate(
self, prompt: Prompt, **kwargs: Unpack[StepFunParametersDict]
) -> AsyncIterator[ChatCompletionStreamOutput]:
async for stream_output in super().async_stream_generate(prompt, **kwargs):
yield stream_output
2 changes: 2 additions & 0 deletions generate/platforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from generate.platforms.minimax import MinimaxSettings
from generate.platforms.moonshot import MoonshotSettings
from generate.platforms.openai import OpenAISettings
from generate.platforms.stepfun import StepFunSettings
from generate.platforms.yi import YiSettings
from generate.platforms.zhipu import ZhipuSettings

Expand All @@ -29,4 +30,5 @@
'DeepSeekSettings',
'YiSettings',
'PlatformSettings',
'StepFunSettings',
]
12 changes: 12 additions & 0 deletions generate/platforms/stepfun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pydantic import SecretStr
from pydantic_settings import SettingsConfigDict

from generate.platforms.openai_like import OpenAILikeSettings


class StepFunSettings(OpenAILikeSettings):
model_config = SettingsConfigDict(extra='ignore', env_prefix='stepfun_', env_file='.env')

api_key: SecretStr
api_base: str = 'https://api.stepfun.com/v1'
platform_url: str = 'https://platform.stepfun.com/'
2 changes: 1 addition & 1 deletion generate/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.4.2'
__version__ = '0.4.3'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "generate-core"
version = "0.4.2"
version = "0.4.3"
description = "文本生成,图像生成,语音生成"
authors = ["wangyuxin <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 452cb46

Please sign in to comment.