小智头像图片
AI教程 2025年01月14日
0 收藏 0 点赞 268 浏览 3858 个字
摘要 :

面向开发者的LLM入门课程-语言模型,提问范式英文版: 1.语言模型 response = get_completion(“What is the capital of China?”) print(response) The capi……

哈喽!伙伴们,我是小智,你们的AI向导。欢迎来到每日的AI学习时间。今天,我们将一起深入AI的奇妙世界,探索“面向开发者的LLM入门课程-语言模型,提问范式英文版”,并学会本篇文章中所讲的全部知识点。还是那句话“不必远征未知,只需唤醒你的潜能!”跟着小智的步伐,我们终将学有所成,学以致用,并发现自身的更多可能性。话不多说,现在就让我们开始这场激发潜能的AI学习之旅吧。

面向开发者的LLM入门课程-语言模型,提问范式英文版

面向开发者的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}

面向开发者的LLM入门课程-评估输入~分类
面向开发者的LLM入门课程-评估输入~分类:在处理不同情况下的多个独立指令集的任务时,首先对查询类型进行分类,并以此为基础确定要使用...

嘿,伙伴们,今天我们的AI探索之旅已经圆满结束。关于“面向开发者的LLM入门课程-语言模型,提问范式英文版”的内容已经分享给大家了。感谢你们的陪伴,希望这次旅程让你对AI能够更了解、更喜欢。谨记,精准提问是解锁AI潜能的钥匙哦!如果有小伙伴想要了解学习更多的AI知识,请关注我们的官网“AI智研社”,保证让你收获满满呦!

微信打赏二维码 微信扫一扫

支付宝打赏二维码 支付宝扫一扫

版权: 转载请注明出处:https://www.ai-blog.cn/2533.html

相关推荐

AI写作-百万级提示词拆解:4 步写出“人模同谋”: 别再让大模型“扮演专家”了——它根本不需要角色,只…

小智头像图片
97

Kimi K2 发布并开源,擅长代码与 Agentic 任务: 今天,我们正式发布 Kimi K2 模型,并同步开源。 …

小智头像图片
167

AI写作-小说大纲在小说中的作用: 大纲主要讲两个部分: 一、大纲有什么用。 二、如何写大纲。 我上…

小智头像图片
177

AI写作-小说大纲的写法和新手常见的问题: 对于大纲的问题,相信大家深深浅浅的都有些了解。今天结…

小智头像图片
268

AI写作-小说的基础提纲结构: 很多的新作者在推出了大量的作品后,却是留给读者的是迷茫,包括作者…

小智头像图片
268

AI写作-小说大纲详解: 案例一:第 1-2 章(通过的大纲分章) 涵柔睁开眼睛,发现正被一个脑满肠肥的…

小智头像图片
268

AI写作-小说大纲格式与内容概述: 什么叫大纲?大纲是一本小说的根本。他为小说提供了精神主旨,主要…

小智头像图片
268

AI写作-如何利用日常生活写小说: 一、举例 1.身边之人是熟人,但是是关系平平的同事, 这种的大家…

小智头像图片
179
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力原创内容

快速提升站内名气成为大牛

扫描二维码

手机访问本站

二维码
vip弹窗图片