-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
OctoAI support #1691
Closed
Closed
OctoAI support #1691
Changes from all commits
Commits
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
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
from typing import Optional | ||
|
||
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler | ||
from langchain_community.llms.octoai_endpoint import OctoAIEndpoint | ||
|
||
from embedchain.config import BaseLlmConfig | ||
from embedchain.helpers.json_serializable import register_deserializable | ||
from embedchain.llm.base import BaseLlm | ||
|
||
|
||
@register_deserializable | ||
class OctoAILlm(BaseLlm): | ||
def __init__(self, config: Optional[BaseLlmConfig] = None): | ||
assert "OCTOAI_API_TOKEN" in os.environ or config.api_key, \ | ||
"Please set OCTOAI_API_TOKEN as environment variable." | ||
super().__init__(config=config) | ||
|
||
def get_llm_model_answer(self, prompt): | ||
return self._get_answer(prompt, self.config) | ||
|
||
@staticmethod | ||
def _get_answer(prompt: str, config: BaseLlmConfig) -> str: | ||
|
||
|
||
octoai_api_key = os.getenv("OCTOAI_API_TOKEN") or config.api_key | ||
callbacks = config.callbacks if (not config.stream) or (config.stream and config.callbacks) \ | ||
else [StreamingStdOutCallbackHandler()] | ||
|
||
chat = OctoAIEndpoint( | ||
octoai_api_token=octoai_api_key, | ||
model_name=config.model, | ||
max_tokens=config.max_tokens, | ||
temperature=config.temperature, | ||
top_p=config.top_p, | ||
streaming=config.stream, | ||
callbacks=callbacks, | ||
) | ||
|
||
return chat.invoke(prompt) |
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import pytest | ||
from embedchain.config import BaseLlmConfig | ||
|
||
from embedchain.llm.octoai import OctoAILlm | ||
|
||
@pytest.fixture | ||
def octoai_env(): | ||
os.environ["OCTOAI_API_TOKEN"] = "test_api_token" | ||
yield | ||
del os.environ["OCTOAI_API_TOKEN"] | ||
|
||
@pytest.fixture | ||
def octoai_llm_config(): | ||
config = BaseLlmConfig( | ||
temperature=0.7, | ||
model="llama-2-13b-chat-fp16", | ||
max_tokens=50, | ||
top_p=0.9, | ||
) | ||
return config | ||
|
||
|
||
def test_get_answer(octoai_llm_config, octoai_env, mocker): | ||
mocked_get_answer = mocker.patch("embedchain.llm.octoai.OctoAILlm._get_answer", return_value="Test answer") | ||
|
||
octoai_llm = OctoAILlm(octoai_llm_config) | ||
answer = octoai_llm.get_llm_model_answer("Test query") | ||
|
||
assert answer == "Test answer" | ||
mocked_get_answer.assert_called_once() | ||
|
||
def test_octo_env_variable(octoai_llm_config): | ||
|
||
with pytest.raises(AssertionError): | ||
_ = OctoAILlm(octoai_llm_config) |
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.
Appreciate adding the assert statment. But can you please also add this: