Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add stepfun integrations #17514

Merged
merged 9 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/api_reference/llms/stepfun.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
::: llama_index.llms.stepfun
options:
members:
- StepFun
153 changes: 153 additions & 0 deletions llama-index-integrations/llms/llama-index-llms-stepfun/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
llama_index/_static
.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
etc/
include/
lib/
lib64/
parts/
sdist/
share/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints
notebooks/

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Jetbrains
.idea
modules/
*.swp

# VsCode
.vscode

# pipenv
Pipfile
Pipfile.lock

# pyright
pyrightconfig.json
3 changes: 3 additions & 0 deletions llama-index-integrations/llms/llama-index-llms-stepfun/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry_requirements(
name="poetry",
)
17 changes: 17 additions & 0 deletions llama-index-integrations/llms/llama-index-llms-stepfun/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)

help: ## Show all Makefile targets.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'

format: ## Run code autoformatters (black).
pre-commit install
git ls-files | xargs pre-commit run black --files

lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files

test: ## Run tests via pytest.
pytest tests

watch-docs: ## Build and watch documentation.
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
51 changes: 51 additions & 0 deletions llama-index-integrations/llms/llama-index-llms-stepfun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# LlamaIndex LLM Integration: Stepfun

## Installation

To install the required packages, run:

```bash
%pip install llama-index-llms-stepfun
!pip install llama-index
```

## Setup

### Initialize StepFun LLM

First, import the necessary libraries and set up your `StepFun` instance. Replace `step-1v-8k`, and `TOKEN` with your model name, and API key, respectively:

```python
import os
from typing import List, Optional
from llama_index.llms.stepfun import StepFun
from llama_index.core.llms import ChatMessage

llm = StepFun(
api_key="TOKEN",
max_tokens=256,
context_window=4096,
model="step-1v-8k",
)
```

## Chat Functionality

StepFun supports chat APIs, allowing you to handle conversation-like interactions. Here’s how to use it:

```python
from llama_index.llms.stepfun import StepFun
from llama_index.core.llms import ChatMessage

llm = StepFun(
api_key="",
max_tokens=256,
context_window=4096,
model="step-1v-8k",
)


message = ChatMessage(role="user", content="Tell me a joke")
resp = llm.chat([message])
print(resp)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from llama_index.llms.stepfun.base import StepFun

__all__ = ["StepFun"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from typing import Any, Dict, Optional

from llama_index.core.base.llms.types import LLMMetadata
from llama_index.core.bridge.pydantic import Field
from llama_index.core.constants import (
DEFAULT_CONTEXT_WINDOW,
DEFAULT_NUM_OUTPUTS,
DEFAULT_TEMPERATURE,
)
from llama_index.core.base.llms.generic_utils import get_from_param_or_env
from llama_index.llms.openai_like import OpenAILike

DEFAULT_API_BASE = "https://api.stepfun.com/v1"
DEFAULT_MODEL = "step-1v-8k"


class StepFun(OpenAILike):
"""
The StepFun class is a subclass of OpenAILike and is used to interact with the StepFun model.

Parameters:
model (str): The name of the Stepfun model to use. See https://platform.stepfun.com/docs/llm/modeloverview for options.
context_window (int): The maximum size of the context window for the model. See https://platform.stepfun.com/docs/llm/modeloverview for options.
is_chat_model (bool): Indicates whether the model is a chat model.

Attributes:
model (str): The name of the Stepfun model to use.
context_window (int): The maximum size of the context window for the model.
is_chat_model (bool): Indicates whether the model is a chat model.
"""

model: str = Field(
description="The Stepfun model to use. See https://platform.stepfun.com/docs/llm/modeloverview for options."
)
context_window: int = Field(
default=DEFAULT_CONTEXT_WINDOW,
description="The maximum number of context tokens for the model. See https://platform.stepfun.com/docs/llm/modeloverview for options.",
gt=0,
)
is_chat_model: bool = Field(
default=True,
description=LLMMetadata.model_fields["is_chat_model"].description,
)

def __init__(
self,
model: str = DEFAULT_MODEL,
temperature: float = DEFAULT_TEMPERATURE,
max_tokens: int = DEFAULT_NUM_OUTPUTS,
additional_kwargs: Optional[Dict[str, Any]] = None,
max_retries: int = 5,
api_base: Optional[str] = DEFAULT_API_BASE,
api_key: Optional[str] = None,
**kwargs: Any,
) -> None:
"""
Initialize the OpenAI API client.

Args:
model (str): The name of the model to use. Defaults to DEFAULT_MODEL.
temperature (float): The temperature to use for the model. Defaults to DEFAULT_TEMPERATURE.
max_tokens (int): The maximum number of tokens to generate. Defaults to DEFAULT_NUM_OUTPUTS.
additional_kwargs (Optional[Dict[str, Any]]): Additional keyword arguments to pass to the model. Defaults to None.
max_retries (int): The maximum number of retries to make when calling the API. Defaults to 5.
api_base (Optional[str]): The base URL for the API. Defaults to DEFAULT_API_BASE.
api_key (Optional[str]): The API key to use. Defaults to None.
**kwargs (Any): Additional keyword arguments to pass to the model.

Returns:
None
"""
additional_kwargs = additional_kwargs or {}

api_base = get_from_param_or_env("api_base", api_base, "STEPFUN_API_BASE")
api_key = get_from_param_or_env("api_key", api_key, "STEPFUN_API_KEY")

super().__init__(
model=model,
temperature=temperature,
max_tokens=max_tokens,
api_base=api_base,
api_key=api_key,
additional_kwargs=additional_kwargs,
max_retries=max_retries,
**kwargs,
)

@classmethod
def class_name(cls) -> str:
return "Stpefun_LLM"
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core"]

[tool.codespell]
check-filenames = true
check-hidden = true
skip = "*.csv,*.html,*.json,*.jsonl,*.pdf,*.txt,*.ipynb"

[tool.llamahub]
contains_example = false
import_path = "llama_index.llms.stepfun"

[tool.llamahub.class_authors]
StepFun = "llama-index"

[tool.mypy]
disallow_untyped_defs = true
exclude = ["_static", "build", "examples", "notebooks", "venv"]
ignore_missing_imports = true
python_version = "3.9"

[tool.poetry]
authors = ["Bestony <[email protected]>"]
description = "llama-index llms stepfun integration"
exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-llms-stepfun"
readme = "README.md"
version = "0.1.0"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
llama-index-llms-openai-like = "^0.3.1"
llama-index-core = "^0.12.0"

[tool.poetry.group.dev.dependencies]
ipython = "8.10.0"
jupyter = "^1.0.0"
mypy = "0.991"
pre-commit = "3.2.0"
pylint = "2.15.10"
pytest = "7.2.1"
pytest-mock = "3.11.1"
ruff = "0.0.292"
tree-sitter-languages = "^1.8.0"
types-Deprecated = ">=0.1.0"
types-PyYAML = "^6.0.12.12"
types-protobuf = "^4.24.0.4"
types-redis = "4.5.5.0"
types-requests = "2.28.11.8"
types-setuptools = "67.1.0.0"

[tool.poetry.group.dev.dependencies.black]
extras = ["jupyter"]
version = "<=23.9.1,>=23.7.0"

[tool.poetry.group.dev.dependencies.codespell]
extras = ["toml"]
version = ">=v2.2.6"

[[tool.poetry.packages]]
include = "llama_index/"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_tests()
Empty file.
Loading
Loading