Skip to content

Conversation

@Sunny-bot1
Copy link
Collaborator

@Sunny-bot1 Sunny-bot1 commented Jul 28, 2025

功能描述

支持 bad_words,用于禁止模型生成某些特定词。通过将bad_words中的词语的采样概率设置为1e-10来避免生成该词。

使用方法

online inference

请求中加入bad_words参数

  • 使用 curl 命令发送用户请求示例如下:
curl -X POST "http://0.0.0.0:9222/v1/chat/completions" \
-H "Content-Type: application/json" \
-d '{
  "messages": [
    {"role": "user", "content": "How old are you"}
  ],
  "bad_words": ["age", "I"]
}'
  • 使用 python 脚本发送用户请求示例如下:
import openai
host = "0.0.0.0"
port = "8170"
client = openai.Client(base_url=f"http://{host}:{port}/v1", api_key="null")

response = client.chat.completions.create(
    model="null",
    messages=[
        {"role": "system", "content": "I'm a helpful AI assistant."},
    ],
    extra_body={"bad_words": ["you", "me"]},
    stream=True,
)
for chunk in response:
    if chunk.choices[0].delta:
        print(chunk.choices[0].delta.content, end='')
print('\n')

offline inference

sampling_params = SamplingParams(bad_words=["you", "me"])
llm = LLM(model= MODEL_DIR)
outputs = llm.generate(PROMPTS, sampling_params)

@paddle-bot
Copy link

paddle-bot bot commented Jul 28, 2025

Thanks for your contribution!

# Update bad tokens len
max_bad_tokens_len = (
paddle.max(self.share_inputs["bad_tokens_len"])
if paddle.max(self.share_inputs["bad_tokens_len"]) > 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的写法可以优化一下,可能会执行两次max算子,可以在初始化时把bad_tokens_len初始值填充为1

continue

if prompt_token_ids not in self._bad_words_token_ids:
self._bad_words_token_ids.extend(prompt_token_ids)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要去重吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要吧,如果给的bad_words有重复的话

@yuanlehome yuanlehome merged commit 74aa31d into PaddlePaddle:develop Jul 30, 2025
10 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants