如何将变量传递给提示并按原样在 Python 中打印它

问题描述 投票:0回答:1

我正在开发一个项目,我需要将变量传递给提示并将其作为输出字符串的一部分打印。我正在使用 Python 和 OpenAI API。具体来说,我有一个函数可以识别给定文本中的非包含代词。识别出的代词存储在一个变量中,我想将其包含在提示字符串中以供进一步处理。

这是我的代码的简化版本:

import re

def identify_non_inclusive_pronouns(text):
    pronouns = ['he', 'she', 'his', 'him', 'her', 'she\'s']
    # Remove text inside parentheses
    text = re.sub(r'\(.*?\)', '', text)
    words = re.findall(r'\b\w+\b', text.lower())
    non_inclusive_pronouns = [word for word in words if word in pronouns]
    unique_non_inclusive_pronouns = list(set(non_inclusive_pronouns))
    return unique_non_inclusive_pronouns


def classify_sentences(message: str):
    non_inclusive_pronouns = identify_non_inclusive_pronouns(message)
    print(non_inclusive_pronouns)
    non_inclusive_pronouns_str = ', '.join(non_inclusive_pronouns)
    print(non_inclusive_pronouns_str)
    system_content = '''
    __CONTEXT__:
    You are a World-Class Inclusive Language Checker, tasked with evaluating English messages for any type of bias. Your objective is to ensure the language used is inclusive, avoiding any words or phrases that could offend any community, ethnic group, race, gender, religious group, socio-economic status, or societal segment.You will be working for the corporate affairs team of Mars Wrigley, a pet care and confectionery giant, to ensure that the posts on various social media platforms are inclusive and respectful.The language should be neutral ,respectable and relatable to everyone inclusive of individuals of any gender,race, caste, creed, religious or socio-economic community. Accurate inclusivity detection is crucial as failure to detect correctly would negatively impact business users and content consumers and will cost the human race.
    __ASK__: 
    Classify the message as "Inclusive" or "Not Inclusive".
    Output for "Not Inclusive" messages:
    Not Inclusive Words: A list of non-inclusive words or phrases.
    Reasons: Detailed explanations for why each word or phrase flagged is considered non-inclusive with inclusive reccomendations.
    __CONSTRAINTS__:
    1. Ensure the model scans the entire text for all predefined inclusivity rules.
    2. Each rule should be independently checked so that multiple issues can be identified within the same passage.Even if one rule is violated the  final outcome is non inclusive and reasons and non inclusivity words should be listed appropriately.
    3. For each instance of non-inclusivity, add the specific non-inclusive words and the corresponding reasons on why the text was flagged and recommendations to the output lists.
    4.Always add a comment under reasons even if the text is inclusive.There are some good to have comments or reccomendation to the user suggested by most of the  rules.

    Given the MESSAGE, let's go step by step using rules to learn how to check if the message is inclusive or not:

    1. **Important Rule:** Retrieval of pronoun variable
        Expected Output:
        - Tag: Not Inclusive
        - not_inclusive_words: Append content of string {non_inclusive_pronouns_str} to this list
        - Reasons: Include the appropriate pronouns in brackets next to 'name/job title' the first time they are mentioned in the message/post.

        OUTPUT:
        {{
        "tag": "Not Inclusive",
        "not_inclusive_words": {non_inclusive_pronouns},
        "reasons": ["Include the appropriate pronouns in brackets next to 'name/job title' the first time they are mentioned in the message/post."]
        }}
    '''
    # Processing with the model here...

即使我按照上面的方法做了之后,我也没有让法学硕士正确打印检索到的代词和标签列表。因为它是基于规则的,所以我希望它 100% 都能工作。请注意,我肯定想将代词传递给提示,因为我希望法学硕士能够对其进行推理。我正在使用 GPT-4o。

python gpt-4
1个回答
0
投票

使用 f 字符串将变量插入文本中。由于 f 字符串使用大括号来表示变量,但您的文本也有文字大括号,因此您必须使用双大括号来转义它们。

示例:

test_var = ['his', 'her', 'he', 'she']

print(f'''Big
Multiline
string
with braces
{{{{{test_var}}}}}
''')

输出:

Big
Multiline
string
with braces
{{['his', 'her', 'he', 'she']}}
© www.soinside.com 2019 - 2024. All rights reserved.