forked from phoenix-oss/llama-stack-mirror
fix: replace eval with json decoding (#1327)
# What does this PR do? - Using `eval` on server is a security risk - Replace `eval` with `json.loads` [//]: # (If resolving an issue, uncomment and update the line below) [//]: # (Closes #[issue-number]) ## Test Plan ``` pytest -v -s --nbval-lax ./llama-stack/docs/notebooks/Llama_Stack_Benchmark_Evals.ipynb ``` <img width="747" alt="image" src="https://github.com/user-attachments/assets/7aff3d95-0b12-4394-b9d0-aeff791eee38" /> [//]: # (## Documentation)
This commit is contained in:
parent
56798fbdda
commit
31c9c6c62f
1 changed files with 5 additions and 5 deletions
|
@ -3,6 +3,7 @@
|
||||||
#
|
#
|
||||||
# This source code is licensed under the terms described in the LICENSE file in
|
# This source code is licensed under the terms described in the LICENSE file in
|
||||||
# the root directory of this source tree.
|
# the root directory of this source tree.
|
||||||
|
import json
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
@ -117,7 +118,7 @@ class MetaReferenceEvalImpl(
|
||||||
generations = []
|
generations = []
|
||||||
for i, x in tqdm(enumerate(input_rows)):
|
for i, x in tqdm(enumerate(input_rows)):
|
||||||
assert ColumnName.chat_completion_input.value in x, "Invalid input row"
|
assert ColumnName.chat_completion_input.value in x, "Invalid input row"
|
||||||
input_messages = eval(str(x[ColumnName.chat_completion_input.value]))
|
input_messages = json.loads(x[ColumnName.chat_completion_input.value])
|
||||||
input_messages = [UserMessage(**x) for x in input_messages]
|
input_messages = [UserMessage(**x) for x in input_messages]
|
||||||
|
|
||||||
# NOTE: only single-turn agent generation is supported. Create a new session for each input row
|
# NOTE: only single-turn agent generation is supported. Create a new session for each input row
|
||||||
|
@ -159,7 +160,7 @@ class MetaReferenceEvalImpl(
|
||||||
generations = []
|
generations = []
|
||||||
for x in tqdm(input_rows):
|
for x in tqdm(input_rows):
|
||||||
if ColumnName.completion_input.value in x:
|
if ColumnName.completion_input.value in x:
|
||||||
input_content = eval(str(x[ColumnName.completion_input.value]))
|
input_content = json.loads(x[ColumnName.completion_input.value])
|
||||||
response = await self.inference_api.completion(
|
response = await self.inference_api.completion(
|
||||||
model=candidate.model,
|
model=candidate.model,
|
||||||
content=input_content,
|
content=input_content,
|
||||||
|
@ -167,9 +168,8 @@ class MetaReferenceEvalImpl(
|
||||||
)
|
)
|
||||||
generations.append({ColumnName.generated_answer.value: response.completion_message.content})
|
generations.append({ColumnName.generated_answer.value: response.completion_message.content})
|
||||||
elif ColumnName.chat_completion_input.value in x:
|
elif ColumnName.chat_completion_input.value in x:
|
||||||
chat_completion_input_str = str(x[ColumnName.chat_completion_input.value])
|
chat_completion_input_json = json.loads(x[ColumnName.chat_completion_input.value])
|
||||||
input_messages = eval(chat_completion_input_str)
|
input_messages = [UserMessage(**x) for x in chat_completion_input_json]
|
||||||
input_messages = [UserMessage(**x) for x in input_messages]
|
|
||||||
messages = []
|
messages = []
|
||||||
if candidate.system_message:
|
if candidate.system_message:
|
||||||
messages.append(candidate.system_message)
|
messages.append(candidate.system_message)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue