面向开发者的LLM入门课程-语言模型,提问范式英文版: 1.语言模型 response = get_completion(“What is the capital of China?”) print(response) The capi……
哈喽!伙伴们,我是小智,你们的AI向导。欢迎来到每日的AI学习时间。今天,我们将一起深入AI的奇妙世界,探索“面向开发者的LLM入门课程-语言模型,提问范式英文版”,并学会本篇文章中所讲的全部知识点。还是那句话“不必远征未知,只需唤醒你的潜能!”跟着小智的步伐,我们终将学有所成,学以致用,并发现自身的更多可能性。话不多说,现在就让我们开始这场激发潜能的AI学习之旅吧。
面向开发者的LLM入门课程-语言模型,提问范式英文版:
1.语言模型
response = get_completion(“What is the capital of China?”)
print(response)
The capital of China is Beijing.
2.Tokens
response = get_completion(“Take the letters in lollipop and reverse them”)
print(response)
The reversed letters of “lollipop” are “pillipol”.
response = get_completion(“””Take the letters in
l-o-l-l-i-p-o-p and reverse them”””)
print(response)
p-o-p-i-l-l-o-l
3.提问范式
def get_completion_from_messages(messages,
model=”gpt-3.5-turbo”,
temperature=0,
max_tokens=500):
”’
封装一个支持更多参数的自定义访问 OpenAI GPT3.5 的函数
参数:
messages: 这是一个消息列表,每个消息都是一个字典,包含 role(角色)和 content(内容)。角
色可以是’system’、’user’ 或 ‘assistant’,内容是角色的消息。
model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4
temperature: 这决定模型输出的随机程度,默认为0,表示输出将非常确定。增加温度会使输出更随
机。
max_tokens: 这决定模型输出的最大的 token 数。
”’
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # 这决定模型输出的随机程度
max_tokens=max_tokens, # 这决定模型输出的最大的 token 数
)
return response.choices[0].message[“content”]
messages = [
{‘role’:’system’,
‘content’:”””You are an assistant who
responds in the style of Dr Seuss.”””},
{‘role’:’user’,
‘content’:”””write me a very short poem
about a happy carrot”””},
]
response = get_completion_from_messages(messages, temperature=1)
print(response)
Oh, a carrot so happy and bright,
With a vibrant orange hue, oh what a sight!
It grows in the garden, so full of delight,
A veggie so cheery, it shines in the light.
Its green leaves wave with such joyful glee,
As it dances and sways, so full of glee.
With a crunch when you bite, so wonderfully sweet,
This happy little carrot is quite a treat!
From the soil, it sprouts, reaching up to the sky,
With a joyous spirit, it can’t help but try.
To bring smiles to faces and laughter to hearts,
This happy little carrot, a work of art!
# length
messages = [
{‘role’:’system’,
‘content’:’All your responses must be
one sentence long.’},
{‘role’:’user’,
‘content’:’write me a story about a happy carrot’},
]
response = get_completion_from_messages(messages, temperature =1)
print(response)
Once upon a time, there was a happy carrot named Crunch who lived in a beautiful
vegetable garden.
# combined
messages = [
{‘role’:’system’,
‘content’:”””You are an assistant who
responds in the style of Dr Seuss.
All your responses must be one sentence long.”””},
{‘role’:’user’,
‘content’:”””write me a story about a happy carrot”””},
]
response = get_completion_from_messages(messages,
temperature =1)
print(response)
Once there was a carrot named Larry, he was jolly and bright orange, never wary.
def get_completion_and_token_count(messages,
model=”gpt-3.5-turbo”,
temperature=0,
max_tokens=500):
“””
使用 OpenAI 的 GPT-3 模型生成聊天回复,并返回生成的回复内容以及使用的 token 数量。
参数:
messages: 聊天消息列表。
model: 使用的模型名称。默认为”gpt-3.5-turbo”。
temperature: 控制生成回复的随机性。值越大,生成的回复越随机。默认为 0。
max_tokens: 生成回复的最大 token 数量。默认为 500。
返回:
content: 生成的回复内容。
token_dict: 包含’prompt_tokens’、’completion_tokens’和’total_tokens’的字典,分别
表示提示的 token 数量、生成的回复的 token 数量和总的 token 数量。
“””
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
content = response.choices[0].message[“content”]
token_dict = {
‘prompt_tokens’:response[‘usage’][‘prompt_tokens’],
‘completion_tokens’:response[‘usage’][‘completion_tokens’],
‘total_tokens’:response[‘usage’][‘total_tokens’],
}
return content, token_dict
messages = [
{‘role’:’system’,
‘content’:”””You are an assistant who responds
in the style of Dr Seuss.”””},
{‘role’:’user’,
‘content’:”””write me a very short poem
about a happy carrot”””},
]
response, token_dict = get_completion_and_token_count(messages)
print(response)
Oh, the happy carrot, so bright and orange,
Grown in the garden, a joyful forage.
With a smile so wide, from top to bottom,
It brings happiness, oh how it blossoms!
In the soil it grew, with love and care,
Nourished by sunshine, fresh air to share.
Its leaves so green, reaching up so high,
A happy carrot, oh my, oh my!
With a crunch and a munch, it’s oh so tasty,
Filled with vitamins, oh so hasty.
A happy carrot, a delight to eat,
Bringing joy and health, oh what a treat!
So let’s celebrate this veggie so grand,
With a happy carrot in each hand.
For in its presence, we surely find,
A taste of happiness, one of a kind!
print(token_dict)
{‘prompt_tokens’: 37, ‘completion_tokens’: 164, ‘total_tokens’: 201}
嘿,伙伴们,今天我们的AI探索之旅已经圆满结束。关于“面向开发者的LLM入门课程-语言模型,提问范式英文版”的内容已经分享给大家了。感谢你们的陪伴,希望这次旅程让你对AI能够更了解、更喜欢。谨记,精准提问是解锁AI潜能的钥匙哦!如果有小伙伴想要了解学习更多的AI知识,请关注我们的官网“AI智研社”,保证让你收获满满呦!
还没有评论呢,快来抢沙发~