Open yithuang123 opened 1 hour ago
To resolve the TypeError: construct_payload() got an unexpected keyword argument 'formatted'
when using the query()
function in your custom LLM implementation, you need to ensure that the arguments you are passing to the construct_payload
method are correct and supported by its definition.
In your MLServerLLM
class, the construct_payload
method does not accept any additional keyword arguments (**kwargs
). However, in the complete
method, you are passing **kwargs
to construct_payload
, which is causing the error.
Here is how you can adjust your code to avoid the error:
construct_payload
method to accept **kwargs
if you need to pass additional arguments.construct_payload
.Here is an example of how you might adjust your code:
class MLServerLLM(CustomLLM):
context_window: int = 3900
num_output: int = 256
model_name: str = "test"
dummy_request: str = "hello"
dummy_response: str = "My response"
@property
def metadata(self) -> LLMMetadata:
"""Get LLM metadata."""
return LLMMetadata(
context_window=self.context_window,
num_output=self.num_output,
model_name=self.model_name,
)
def construct_payload(self, prompt, **kwargs):
template = {
"parameters": {
"extra": {
"max_new_tokens": 256,
"temperature": 0.3,
"repetition_penalty": 1.0
}
},
"inputs": [
{
"name": "input",
"shape": [1],
"datatype": "str",
"data": ["hi"]
}
]
}
template["inputs"][0]["data"] = [prompt]
return template
def construct_url(self):
return _LLM_URL.format(model_name=self.model_name)
@staticmethod
def _parse_response(resp):
if resp.status_code != 200:
raise Exception(resp)
return resp.json()["outputs"][0]["data"][0]
@llm_completion_callback()
def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
response = self._parse_response(requests.post(url=self.construct_url(),
json=self.construct_payload(prompt, **kwargs),
headers={"Content-Type": "application/json"},
params={}))
return CompletionResponse(text=response)
@llm_completion_callback()
def stream_complete(
self, prompt: str, **kwargs: Any
) -> CompletionResponseGen:
response = ""
for token in self.dummy_response:
response += token
yield CompletionResponse(text=response, delta=token)
By updating the construct_payload
method to accept **kwargs
, you ensure that any additional arguments passed from the complete
method do not cause an error. This should resolve the TypeError
you are encountering [1].
To continue talking to Dosu, mention @dosu.
Bug Description
Want to leverage custom LLM and custom embedding to build basic RAG. custom LLM and embedding complete as expected when i am testing. But when i tried to use query() function, got an TypeError: construct_payload() got an unexpected keyword argument 'formatted'.
Version
0.11.8
Steps to Reproduce
Relevant Logs/Tracbacks
No response