-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PiperOrigin-RevId: 572718586
- Loading branch information
Showing
6 changed files
with
152 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright 2023 The Langfun Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Language models from llama.cpp.""" | ||
|
||
from typing import Annotated | ||
|
||
import langfun.core as lf | ||
import requests | ||
|
||
|
||
@lf.use_init_args(["url"]) | ||
class LlamaCppRemote(lf.LanguageModel): | ||
"""The remote LLaMA C++ model. | ||
The Remote LLaMA C++ models can be launched via | ||
https://github.com/ggerganov/llama.cpp/tree/master/examples/server | ||
""" | ||
|
||
url: Annotated[ | ||
str, | ||
"The name of the model to use.", | ||
] = "" | ||
|
||
name: Annotated[ | ||
str, | ||
"The abbreviation for the LLaMA CPP-based model name.", | ||
] = "" | ||
|
||
@property | ||
def model_id(self) -> str: | ||
"""Returns a string to identify the model.""" | ||
return f"LLaMAC++({self.name})" | ||
|
||
def _sample(self, prompts: list[lf.Message]) -> list[lf.LMSamplingResult]: | ||
def _complete_fn(cur_prompts): | ||
results = [] | ||
for prompt in cur_prompts: | ||
result = lf.LMSamplingResult() | ||
for _ in range(self.sampling_options.n or 1): | ||
data = { | ||
"prompt": prompt.text, | ||
"n_predict": self.sampling_options.max_tokens, | ||
"temperature": self.sampling_options.temperature, | ||
"top_k": self.sampling_options.top_k or 50, | ||
"top_p": self.sampling_options.top_p or 0.95, | ||
} | ||
response = requests.post( | ||
f"{self.url}/completion", | ||
json=data, | ||
headers={"Content-Type": "application/json"}, | ||
timeout=self.timeout, | ||
) | ||
decoded_response = response.json() | ||
response = decoded_response["content"] | ||
result.samples.append(lf.LMSample(response, score=0.0)) | ||
results.append(result) | ||
return results | ||
|
||
return lf.with_retry( | ||
_complete_fn, | ||
retry_on_errors=(), | ||
max_attempts=self.max_attempts, | ||
retry_interval=(1, 60), | ||
exponential_backoff=True, | ||
)(prompts) |
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,56 @@ | ||
# Copyright 2023 The Langfun Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests for llama cpp models.""" | ||
|
||
import typing | ||
import unittest | ||
from unittest import mock | ||
|
||
import langfun.core as lf | ||
from langfun.core.llms import llama_cpp | ||
|
||
|
||
def mock_requests_post(url: str, json: typing.Dict[str, typing.Any], **kwargs): | ||
del kwargs | ||
|
||
class TEMP: | ||
|
||
def json(self): | ||
return {"content": json["prompt"] + "\n" + url} | ||
|
||
return TEMP() | ||
|
||
|
||
class LlamaCppRemoteTest(unittest.TestCase): | ||
"""Tests for the LlamaCppRemote model.""" | ||
|
||
def test_call_completion(self): | ||
with mock.patch("requests.post") as mock_request: | ||
mock_request.side_effect = mock_requests_post | ||
lm = llama_cpp.LlamaCppRemote(url="http://127.0.0.1:8080") | ||
response = lm("hello", sampling_options=lf.LMSamplingOptions(n=1)) | ||
self.assertEqual( | ||
response.text, | ||
"hello\nhttp://127.0.0.1:8080/completion", | ||
) | ||
|
||
def test_name(self): | ||
lm = llama_cpp.LlamaCppRemote() | ||
self.assertEqual(lm.model_id, "LLaMAC++()") | ||
lm = llama_cpp.LlamaCppRemote(url="xxx", name="x") | ||
self.assertEqual(lm.model_id, "LLaMAC++(x)") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
jinja2>=3.1.2 | ||
openai==0.27.2 | ||
pyglove>=0.4.4.dev20231009 | ||
requests>=2.31.0 | ||
termcolor==1.1.0 | ||
tqdm>=4.64.1 |