首页 > 编程语言 >【第四期书生大模型实战营】第2关 L0G2000 Python 基础知识

【第四期书生大模型实战营】第2关 L0G2000 Python 基础知识

时间:2025-02-09 17:57:32浏览次数:3  
标签:count 第四期 word L0G2000 Python json api ransomNote return

任务

任务概览

任务类型 任务内容 预计耗时
闯关任务 Leetcode 383(笔记中提交代码与leetcode提交通过截图) 20mins
闯关任务 Vscode连接InternStudio debug笔记 10mins
可选任务 pip安装到指定目录 10mins

作业总共分为三个任务,两个闯关任务均完成视作闯关成功。
请将作业发布到知乎、CSDN等任一社交媒体,将作业链接提交到以下问卷,助教老师批改后将获得 50 算力点奖励!!!

提交地址:https://aicarrier.feishu.cn/share/base/form/shrcnUqshYPt7MdtYRTRpkiOFJd

任务一

完成Leetcode 383, 笔记中提交代码与leetcode提交通过截图

任务二

下面是一段调用书生浦语API实现将非结构化文本转化成结构化json的例子,其中有一个小bug会导致报错。请大家自行通过debug功能定位到报错原因。

TIPS:

  • 打断点查看下LLM返回的文本结果。造成本bug的原因与LLM的输出有关,学有余力的同学可以尝试修正这个BUG。

  • 作业提交时需要有debug过程的图文笔记,必须要有打断点在debug中看到res变量的值的截图。

  • 避免将api_key明文写在程序中!!! 本段demo为了方便大家使用debug所以将api_key明文写在代码中,这是一种极其不可取的行为!

from openai import OpenAI
import json
def internlm_gen(prompt,client):
    '''
    LLM生成函数
    Param prompt: prompt string
    Param client: OpenAI client 
    '''
    response = client.chat.completions.create(
        model="internlm2.5-latest",
        messages=[
            {"role": "user", "content": prompt},
      ],
        stream=False
    )
    return response.choices[0].message.content

api_key = ''
client = OpenAI(base_url="https://internlm-chat.intern-ai.org.cn/puyu/api/v1/",api_key=api_key)

content = """
书生浦语InternLM2.5是上海人工智能实验室于2024年7月推出的新一代大语言模型,提供1.8B、7B和20B三种参数版本,以适应不同需求。
该模型在复杂场景下的推理能力得到全面增强,支持1M超长上下文,能自主进行互联网搜索并整合信息。
"""
prompt = f"""
请帮我从以下``内的这段模型介绍文字中提取关于该模型的信息,要求包含模型名字、开发机构、提供参数版本、上下文长度四个内容,以json格式返回。
`{content}`
"""
res = internlm_gen(prompt,client)
res_json = json.loads(res)
print(res_json)

任务三(可选)

使用VScode连接开发机后使用pip install -t命令安装一个numpy到看开发机/root/myenvs目录下,并成功在一个新建的python文件中引用。

一、力扣 383 赎金信问题 https://leetcode.cn/problems/ransom-note/description/

两个字符串构成问题,可以通过逐个字符对比,


class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        for i in range(len(ransomNote)):
            if not ransomNote[i] in magazine:
                return False
            else:
                if randsomNote.count(ransomNote[i])>magazine(randsomNote[i]):
                    return False
                
        return true

时间复杂度高,改成使用字典的形式

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        word_count = {}
        for word in magazine:
            word_count[word]=word_count.get(word,0)+1


        for word in ransomNote:
            if word not in word_count or word_count[word]==0:
                return False
            word_count[word]-=1 
        return True

官网使用collections.Counter取差集,代码更简洁

二、调试代码

安装依赖
pip install OpenAI

运行错误

调试

可以看到是返回的数据不止json字符串,前后都有多余的部分,可以使用split拆分,在用rstrip剔除末尾的多余字符。
修复

标签:count,第四期,word,L0G2000,Python,json,api,ransomNote,return
From: https://www.cnblogs.com/geyee/p/18706378

相关文章