from Pexels (Karolina)

Sure, here's the translation: ```html 减少您的OpenAI API费用高达70% ```

在HTML结构中保持,将以下英文文本翻译为简体中文: 提示工程是你所需要的一切

以下是使用HTML结构的文本: ```html

虽然自从ChatGPT问世以来,许多开源LLM已经出现,但没有一个能够完全匹配OpenAI的满意度和可靠性。如果您想在您的应用程序中使用LLMs或自己构建一个LLM,那么OpenAI仍然是最佳选择。他们为他们的语言模型API提供了出色的商业模式,而他们最便宜且功能强大的选项gpt-3.5-turbo-0125,被广泛使用。但是您实际上可以通过聪明地调用API来使其更便宜。秘诀在于提示工程,知道要放入什么以及您想要得到什么。

```

To translate the given English text into simplified Chinese while keeping the HTML structure intact, you can use the following: ```html

让我们快速看一下今天(2024年3月)的现状。对于这篇博客,我们将使用gpt-3.5-turbo-0125语言模型。你可以选择其他模型,但这个对我来说最实惠,特别是因为我们将处理大量文本数据。让我们来看看它的规格,比如定价、令牌限制等。

``` This HTML code wraps the translated Chinese text in a `

` (paragraph) tag, maintaining the structure of the original content.

Sure, here's the translated text in simplified Chinese, while keeping the HTML structure: ```html

在我们开始之前,让我们看看每次 API 调用时 OpenAI 如何计算令牌。

```
# Import the OpenAI class from the openai module
from openai import OpenAI

# Create an instance of the OpenAI class with the provided API key
client = OpenAI(api_key=open_ai_key)

# Set the user input to the string "Hi"
user_input = "Hi"

# Create a chat completion using the OpenAI client and the user input
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125", # Set the model to "gpt-3.5-turbo-0125"
messages=[{"role": "user", "content": user_input}]
)

# Print the response from the chat completion
print(response.choices[0].message.content)

######## OUTPUT ########
Hello! How can I help you today?
######## OUTPUT ########

Sure, here's the HTML structure with the text translated into simplified Chinese: ```html

你可能会认为,如果你发送“Hi”作为单个令牌输入,响应只会是7到9个令牌。但事实并非如此。当我们打印响应的使用详情时,我们看到的是:

```
# Print the usage of the response
print(response.usage)

######## OUTPUT ########
CompletionUsage(completion_tokens=9, prompt_tokens=8, total_tokens=17)
######## OUTPUT ########

请注意,prompt_tokens指的是输入标记,为8。这是因为每次向API发送文本时,会自动添加额外的7个标记。completion_tokens的值是我们预期的,表示模型生成的响应中的标记。

Sure, here's the translated text in simplified Chinese, while maintaining the HTML structure: ```html

根据我们所学的知识,让我们编写一个函数,该函数以用户提示作为输入,并返回四个值:API 响应、输入令牌的成本、输出令牌的成本和总成本。

```
def openai_chat(user_prompt):

# Create a chat completion using the OpenAI client
response = client.chat.completions.create(
# Set the model to "gpt-3.5-turbo-0125"
model="gpt-3.5-turbo-0125",
messages=[{"role": "user", "content": user_prompt}]
)

# Response from the chat completion
answer = response.choices[0].message.content

# Calculate the price for input tokens
input_price = response.usage.prompt_tokens * (0.5 / 1e6)

# Calculate the price for input tokens
output_price = response.usage.completion_tokens * (1.5 / 1e6)

# calculate the total price
total_price = input_price + output_price

# Return the answer, input price, output price, total price
return {
"answer": answer,
"input_price": f"$ {input_price}",
"output_price": f"$ {output_price}",
"total_price": f"$ {total_price}"
}

Sure, here's the translation in simplified Chinese while keeping the HTML structure: ```html 让我们通过尝试一个简单且简短的提示来看看这个函数是如何工作的。 ```

# Calling our function with the prompt
openai_chat("What is the capital of China?")


####### OUTPUT #######
{
answer: 'The capital of China is Beijing.',
input_price: '$ 7e-06',
output_price: '$ 1.05e-05',
total_price: '$ 1.75e-05'
}
####### OUTPUT #######

我们函数的输出应该是清晰的。只是为了澄清,价格是根据我之前向你展示的gpt-3.5-turbo-0215模型的定价表计算的。

# asking a question regarding 100k words document
openai_chat(100K_words_document_and_one_question)

####### OUTPUT #######
{
answer: 'According to the give context .... ',
input_price: '$ 0.18',
output_price: '$ 0.02',
total_price: '$ 0.20'
}
####### OUTPUT #######

```html

对于大量文本(100,000字)和一个问题,预计的成本仅约为$0.20。这显示了gpt-3.5-turbo模型可以有多有效。现在我们的函数已经准备就绪,让我们看看如何最小化使用OpenAI API的成本。

```

Sure, here's the translation of "Clustering using OpenAI API" in simplified Chinese while maintaining HTML structure: ```html 使用 OpenAI API 进行聚类

使用 OpenAI API 进行聚类

```

Sure, here's the translated text within an HTML structure: ```html

想象一下,你有一个庞大的新闻标题列表,你想要对它们进行聚类。虽然使用嵌入是一种选择,但假设你想要使用OpenAI语言模型API,因为它可以以类似人类的方式捕捉含义。在我们创建提示模板以最小化成本之前,让我们先看一下我们的新闻标题列表是什么样子的。

```
# news heading list
news_headlines = [
"donald trump is ... surprise when he arrives",
"Emirates airlines ... takeover of Etihad Airways",
"The US is considering ... troops to Afghanistan",
...
]

# length of news list
len(news_headlines)


####### OUTPUT #######
1256
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese, while keeping the HTML structure: ```html

我们有超过1200个新闻标题。在我们创建提示模板之前,让我们将它们合并成一个具有特定格式的字符串。这将帮助语言模型更有效地处理它们。

```
formatted_news = "Given the news headlines:\n"

# looping through all news headlines and formatting them in single string
for i in range(0,len(user_input)):
formatted_string += f"s{i}: {user_input[i]}\n"

# printing the formatted news
print(formatted_news)


####### OUTPUT #######
s0: donald trump is ... surprise when he arrives
s1: Emirates airlines ... takeover of Etihad Airways
s2: The US is considering ... troops to Afghanistan
...
s1256: Virat Kholi is ready ... against Srilanka
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese: ```html

我已经用更简短的形式代表了新闻标题,比如s0,s1,s2等等。这样做的原因是,当语言模型对它们进行聚类时,它可以简单地使用这些短缩写(例如,s35代表第35条新闻标题),而不是在每个聚类中写出整个标题。

```
prompt_template = f'''{formatted_news}
i have these news headlines i want you to perform clustering on it.
you have to answer me in this format:
cluster1: s1,s3 ...
cluster2: s2, s6 ...
...
do not write sentences but write it just like this starting from s0,s1,s2 ...
dont say anything else other than the format
'''

下一步,我定义了我的提示模板。该模板指定了我希望语言模型提供的答案格式,以及一些额外的信息以便于理解。关键在于,我们并不要求模型完整地写出标题,而是只需使用简短的缩写。

Sure, here's the translated text in simplified Chinese: 我们所需要做的就是将这个提示模板传递给我们之前创建的函数,然后看看它在定价和响应质量方面的表现如何。

# Calling our function with prompt_template
results = openai_chat(prompt_template)

# printing response only
print(results['answer'])


####### OUTPUT #######
cluster1: s5, s67, s55, s134, s764 ...
cluster2: s64, s21, s896, s445, s12 ...
...
cluster7: s12, s853, s414, s244, s712 ...
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese: ```html 我在这里截断了响应,但语言模型成功地将我们的1256条新闻标题分类为七个簇。您可以进一步编辑提示模板以指定所需的簇数。让我们看看这项任务花费了我们多少。 ```

# printing complete results
print(results)

####### OUTPUT #######
{
answer: 'cluster1: s5, s67, ...',
input_price: '$ 0.020',
output_price: '$ 0.003',
total_price: '$ 0.023'
}
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese: ```html 总成本为$0.023。如果我们没有使用这个提示模板,我们可能会花费的成本是输入成本的两倍以上。这是因为我们将所有的头条作为输入发送,而返回的完整头条的输出成本为每百万个标记$1.50。以下是我们使用这种方法节省了多少成本的摘要: ```

这种方法将至关重要,因为当您处理更大的数据时,它会呈指数级地节省您的学分。但我们必须稍微编写一点代码,将简短的缩写映射到实际的标题上。

# Split the 'answer' string by newline character
responses = result['answer'].split("\n")

# Create a dictionary comprehension to extract key-value pairs from each line
clusters = {line.split(": ")[0].strip(): [i.strip() for i in line.split(": ")[1].split(",")] for line in responses if line.strip()}

# Replace the keys (s01, s02, s03, etc.) with the actual headlines from 'news_headlines'
for k, v in clusters.items():
clusters[k] = [news_headlines[int(i[1:])] for i in v]

以下代码将OpenAI的响应映射回实际新闻标题。以下是clusters字典的样子:

# print clusters
print(clusters)

####### OUTPUT #######
{
'cluster1': ["Sea levels ... change", "Arabian sea .. pollution", ...],
'cluster2': ["fifa has banned ... ", "Ronaldo has been ... a row", ...}
...
}
####### OUTPUT #######

Sure, here's the text translated into simplified Chinese within an HTML structure: ```html 使用 OpenAI API 进行拼写检查

使用 OpenAI API 进行拼写检查

``` This HTML structure includes the translated text "使用 OpenAI API 进行拼写检查" (SpellCheck using OpenAI API) in simplified Chinese.

```html

英文文本翻译

假设您有一篇长篇文本文档,并且希望构建一个语法校正工具作为一个小型网络应用程序。虽然有许多自然语言处理技术可用于此任务,但是语言模型,特别是来自OpenAI的模型,已经接受了大量数据的训练,使它们成为一个潜在的更好选择。再次强调,关键是要在我们的提示模板中策略性地处理。我们希望API响应突出显示不正确的单词,并建议其正确的拼写,而不是提供整个已校正的文本作为输出。让我们来看看我们的输入文本:

```
# my input which contains spelling errors
user_input = '''As the sun beganned to set on the horizen, casting a
warm gloy across the ... '''

# printing total words
print(len(input_text.split()))


####### OUTPUT #######
500,000
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese while maintaining the HTML structure: ```html

我们有一个包含拼写错误的50万字文档需要校对。我们的下一步是创建一个提示模板,引导API提供仅针对有问题的单词的回应。

```
prompt_template = f'''Given the input text:
user input: {user_input}

output must be in this format:
misspelled_word:corrected_word
...

output must not contain any other information than the format
'''

在提示模板中,我们明确定义了所需的响应格式。API应该突出显示拼写错误的单词,并提供它们的正确替换。我们还指定响应中不应包含任何额外信息,因为我们将使用代码来替换原始用户输入中的这些单词。

Sure, here's the translation in simplified Chinese within the HTML structure: ```html

我们所需要做的就是将这个提示模板传递给我们之前创建的函数,然后看看它在定价和响应质量方面的表现。

```
# Calling our function with prompt_template
results = openai_chat(prompt_template)

# printing response only
print(results['answer'])

####### OUTPUT #######
beganned: began
horizen: horizon
gras: grass
...
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese within the HTML structure: ```html

我在这里截断了回应,但语言模型成功识别了拼错的单词并提供了它们的正确形式。让我们看看这个任务花费了我们多少钱。

```
# printing complete results
print(results)

####### OUTPUT #######
{
answer: 'beganned: began \n horizen: horizon ...',
input_price: '$ 0.33333',
output_price: '$ 0.005135',
total_price: '$ 0.33812'
}
####### OUTPUT #######

以下是使用该方法节省的总成本:完成此任务的总成本为0.33812美元,如果500K个字包含8K-10K个拼写错误。如果我们没有使用这个提示模板,我们可能会花费超过两倍于输入成本的金额。这是因为我们将把所有文本作为输入发送,并且每百万个标记的输出成本将应用于完整的输入文本。以下是我们通过使用这种方法节省的总成本的摘要:

Sure, here's the translation: ```html 即使是500字的相对较小文本,成本节省也是显而易见的。想象一下在处理大数据时的财务收益! ```

以下是将简短的缩写映射到实际标题需要进行一些编码。

# Split the 'response1' string by newline character
response = result['answer'].split("\n")

# Create a list comprehension to extract key-value pairs from each line
result = [(line.split(":")[0], line.split(":")[1]) for line in response if line.strip()]

# Replace the misspelled words with the corrected words
for word, corrected_word in result:
corrected_user_input = user_input.replace(word, corrected_word)

以下代码将会将拼写错误的单词与它们的正确版本进行映射:

# print clusters
print(corrected_user_input)

####### OUTPUT #######
As the sun began to set on the horizon, casting
warm glow across the ...
####### OUTPUT #######

Sure, here's the text in simplified Chinese, keeping the HTML structure intact: ```html Text Cleaning using OpenAI API ``` In Chinese characters, it would be: ```html 使用OpenAI API进行文本清洗 ```

Sure, here's the translated text in simplified Chinese within an HTML structure: ```html

我们用于拼写检查的提示模板将用于文本清洗,因为我们只需要突出显示那些要清理或完全删除的单词。

```

Sure, here's the translated text in simplified Chinese while keeping the HTML structure: ```html 我们希望 API 响应突出显示不正确的单词,并建议其正确的拼写,而不是提供整个更正后的文本作为输出。让我们看看我们的输入文本: ```

# my input which contains spelling errors
user_input = '''The rugge9d pathw&ay winded through the th!ck f0r3st,
obfusca+ting the way forward. The creaking of branches
and rustling of leaves added an ... '''

# printing total words
print(len(input_text.split()))

####### OUTPUT #######
1,000,000
####### OUTPUT #######

Sure, here is the translation of the text into simplified Chinese, while keeping the HTML structure intact: ```html 我们有一个需要清理的100万字文档。我们接下来的步骤是创建一个提示模板,指导API提供仅关注问题词汇的响应。 ``` In this translation: - "我们有一个需要清理的100万字文档。" translates to "We have a 1M-word document which requires cleaning." - "我们接下来的步骤是创建一个提示模板,指导API提供仅关注问题词汇的响应。" translates to "Our next step is to create a prompt template that guides the API to provide responses focused solely on the problematic words."

prompt_template = f'''Given the input text:
user input: {user_input}
output must be in this format:
uncleaned_word:cleaned_word
...
if no replacement for uncleaned_word exist then use this format
uncleaned_word:
output must not contain any other information than the format
'''

在提示模板中,我们已明确定义了所需的响应格式。API 应突出显示未清理的单词,并提供它们的正确替换。如果未清理的单词没有替换,那么应该用空字符串替换。我们还指定响应中不应包含额外信息,因为我们将使用代码来替换原始用户输入中的这些单词。

我们只需将此提示模板传递给我们之前创建的函数,然后看看它在定价和响应质量方面的表现如何。

# Calling our function with prompt_template
results = openai_chat(prompt_template)

# printing response only
print(results['answer'])

####### OUTPUT #######
rugge9d: rugged
th!ck: thick
f0r3st: forest
obfusca+ting: obfuscating
s;ense: sense
...
####### OUTPUT #######

Sure, here's the translated text in simplified Chinese, maintaining the HTML structure: ```html

我在这里截断了响应,但语言模型成功识别了未清理的词,并提供了它们的正确形式。让我们看看这项任务花费了我们多少。

```
# printing complete results
print(results)

####### OUTPUT #######
{
answer: 'rugge9d: rugged \n th!ck: thick ...',
input_price: '$ 0.6665',
output_price: '$ 0.099',
total_price: '$ 0.7665'
}
####### OUTPUT #######

Sure, here's the translation in simplified Chinese: 这项任务的总成本为0.7665美元,如果100万字包含20,000至30,000个清理错误。如果我们没有使用这个提示模板,我们可能会花费超过输入成本的两倍。以下是我们使用这种方法节省的金额总结:

在处理100万字的文档时,成本节约非常明显。想象一下在处理大数据时的财务收益!

Sure, here's the translated text in simplified Chinese: ```html 但是我们必须稍微编写一点代码来将简短的缩写映射到实际的标题。 ```

# Split the 'response1' string by newline character
response = result['answer'].split("\n")

# Create a list comprehension to extract key-value pairs from each line
result = [(line.split(": ")[0], line.split(": ")[1]) for line in response if line.strip()]
# Replace the misspelled words with the corrected words
for word, corrected_word in result:
cleaned_user_input = user_input.replace(word, corrected_word)

上面的代码将把拼写错误的单词与其正确版本对应起来:

# print clusters
print(cleaned_user_input)

####### OUTPUT #######
The rugged pathway winded through the th!ck forest,
obfuscating the way forward. The creaking of branches
and rustling of leaves added an ...
####### OUTPUT #######

Sure, here's the translation of "LLM Based NLP" into simplified Chinese while keeping the HTML structure: ```html 基于LLM的自然语言处理 ```

Sure, here's the translation in simplified Chinese while keeping the HTML structure: ```html

我开发了一个使用LLM APIs执行各种任务的NLP库。 它包括超过30个功能,其中许多使用与上述相似的成本优化策略。 您可以在我的GitHub存储库中探索该库及其提示模板:

```

2024-06-11 04:08:06 AI中文站翻译自原文