-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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 solar pro llm and document parser #16099
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
226f621
feat: add solar pro llm and document parser
JuHyung-Son 352cadd
bump DP version
JuHyung-Son 8fbe5ef
Merge branch 'main' into solar-pro
JuHyung-Son 16b8139
remove poetry.lock
JuHyung-Son 7aabd5c
add dependency
JuHyung-Son 4b76ace
add _stream_chat
JuHyung-Son fb11104
Merge branch 'solar-pro' of https://github.com/JuHyung-Son/llama_inde…
JuHyung-Son f64cfd4
Merge branch 'main' into solar-pro
JuHyung-Son a0fd60c
override some methods
JuHyung-Son c3e1383
[docs] update readme and example code for DocumentParse
cyzhzhd 89ec18d
linting
logan-markewich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
from typing import Optional, Dict, Any, Sequence, Callable | ||
import os | ||
from pathlib import Path | ||
from typing import List, Optional, Dict, Any, Sequence, Callable, Union | ||
import warnings | ||
|
||
import httpx | ||
from llama_index.readers.upstage import UpstageDocumentParseReader | ||
from llama_index.llms.openai.utils import ( | ||
from_openai_token_logprobs, | ||
create_retry_decorator, | ||
from_openai_message, | ||
) | ||
from llama_index.core.base.llms.types import ChatResponse | ||
from llama_index.core.base.llms.types import LLMMetadata, ChatMessage | ||
from llama_index.llms.openai import OpenAI | ||
from llama_index.llms.openai.base import to_openai_message_dicts | ||
|
||
from llama_index.llms.upstage.utils import ( | ||
SOLAR_TOKENIZERS, | ||
is_doc_parsing_model, | ||
resolve_upstage_credentials, | ||
is_chat_model, | ||
upstage_modelname_to_contextsize, | ||
|
@@ -14,17 +26,25 @@ | |
from llama_index.core.callbacks import CallbackManager | ||
from llama_index.core.constants import DEFAULT_TEMPERATURE | ||
from llama_index.core.types import BaseOutputParser, PydanticProgramMode | ||
from llama_index.core.bridge.pydantic import ConfigDict | ||
from tokenizers import Tokenizer | ||
from pydantic import Field, PrivateAttr | ||
from pydantic import ConfigDict, Field, PrivateAttr | ||
from openai import OpenAI as SyncOpenAI | ||
from openai import AsyncOpenAI | ||
|
||
DEFAULT_UPSTAGE_MODEL = "solar-1-mini-chat" | ||
|
||
llm_retry_decorator = create_retry_decorator( | ||
max_retries=6, | ||
random_exponential=True, | ||
stop_after_delay_seconds=60, | ||
min_seconds=1, | ||
max_seconds=20, | ||
) | ||
|
||
|
||
class Upstage(OpenAI): | ||
"""Upstage LLM. | ||
""" | ||
Upstage LLM. | ||
|
||
Examples: | ||
`pip install llama-index-llms-upstage` | ||
|
@@ -87,7 +107,7 @@ class Upstage(OpenAI): | |
"Huggingface pretrained tokenizer name " | ||
"upstage opened solar tokenizer in Huggingface. https://huggingface.co/upstage/solar-1-mini-tokenizer" | ||
), | ||
default="upstage/solar-1-mini-tokenizer", | ||
default=SOLAR_TOKENIZERS[DEFAULT_UPSTAGE_MODEL], | ||
) | ||
|
||
api_key: str = Field( | ||
|
@@ -124,7 +144,7 @@ def __init__( | |
completion_to_prompt: Optional[Callable[[str], str]] = None, | ||
pydantic_program_mode: PydanticProgramMode = PydanticProgramMode.DEFAULT, | ||
output_parser: Optional[BaseOutputParser] = None, | ||
**kwargs: Any | ||
**kwargs: Any, | ||
) -> None: | ||
if "upstage_api_key" in kwargs: | ||
api_key = kwargs.pop("upstage_api_key") | ||
|
@@ -153,7 +173,7 @@ def __init__( | |
completion_to_prompt=completion_to_prompt, | ||
pydantic_program_mode=pydantic_program_mode, | ||
output_parser=output_parser, | ||
**kwargs | ||
**kwargs, | ||
) | ||
|
||
self.tokenizer_name = tokenizer_name | ||
|
@@ -187,6 +207,10 @@ def _tokenizer(self) -> Optional[Tokenizer]: | |
""" | ||
Get a Huggingface tokenizer for solar models. | ||
""" | ||
if SOLAR_TOKENIZERS.get(self.model) != self.tokenizer_name: | ||
warnings.warn( | ||
f"You are using a different tokenizer than the one specified in the model. This may cause issues with token counting. Please use {SOLAR_TOKENIZERS[self.model]} as the tokenizer name." | ||
) | ||
return Tokenizer.from_pretrained(self.tokenizer_name) | ||
|
||
def get_num_tokens_from_message(self, messages: Sequence[ChatMessage]) -> int: | ||
|
@@ -207,3 +231,50 @@ def get_num_tokens_from_message(self, messages: Sequence[ChatMessage]) -> int: | |
) | ||
num_tokens += tokens_suffix | ||
return num_tokens | ||
|
||
@llm_retry_decorator | ||
def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse: | ||
if is_doc_parsing_model(self.model, kwargs): | ||
document_contents = self._parse_documents(kwargs.pop("file_path")) | ||
messages.append(ChatMessage(role="user", content=document_contents)) | ||
client = self._get_client() | ||
message_dicts = to_openai_message_dicts(messages) | ||
response = client.chat.completions.create( | ||
messages=message_dicts, | ||
stream=False, | ||
**self._get_model_kwargs(**kwargs), | ||
) | ||
openai_message = response.choices[0].message | ||
message = from_openai_message(openai_message) | ||
openai_token_logprobs = response.choices[0].logprobs | ||
logprobs = None | ||
if openai_token_logprobs and openai_token_logprobs.content: | ||
logprobs = from_openai_token_logprobs(openai_token_logprobs.content) | ||
|
||
return ChatResponse( | ||
message=message, | ||
raw=response, | ||
logprobs=logprobs, | ||
additional_kwargs=self._get_response_token_counts(response), | ||
) | ||
|
||
def _parse_documents( | ||
self, file_path: Union[str, Path, List[str], List[Path]] | ||
) -> str: | ||
document_contents = "Documents:\n" | ||
|
||
loader = UpstageDocumentParseReader( | ||
api_key=self.api_key, output_format="text", coordinates=False | ||
) | ||
docs = loader.load_data(file_path) | ||
|
||
if isinstance(file_path, list): | ||
file_titles = [os.path.basename(path) for path in file_path] | ||
else: | ||
file_titles = [os.path.basename(file_path)] | ||
|
||
for i, doc in enumerate(docs): | ||
file_title = file_titles[min(i, len(file_titles) - 1)] | ||
document_contents += f"{file_title}:\n{doc.text}\n\n" | ||
print("DOCUMENT CONTENTS", document_contents) | ||
return document_contents | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets remove this print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious why we subclass _chat, but not any other method like _stream_chat or _achat, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you only support parse documents with
.chat()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a mistake in following llamaindex interface. Added some more methods.