소스 검색

更换获取文章的方式,改为openai库获取,不使用requests

xie 1 개월 전
부모
커밋
c7e60f802c
3개의 변경된 파일60개의 추가작업 그리고 8개의 파일을 삭제
  1. 52 1
      gpt/chatgpt.py
  2. 6 5
      gpt/get_article2.py
  3. 2 2
      mock/mock_request.py

+ 52 - 1
gpt/chatgpt.py

@@ -92,6 +92,40 @@ def get_answer_from_gpt(question,real_ip="localhost",demo_name="无",model="gpt-
 
     logger.critical("get_answer_from_gpt 严重错误,3次后都失败了")
 
+def get_article_gpt_pydantic(question,real_ip="localhost",demo_name="无",model="gpt-4o",max_tokens=3500,temperature:float=0,n=1,
+                        check_fucn=None,sys_prompt=None):
+   
+    d2 = {"model": model, "messages": [], "max_tokens": max_tokens, "temperature": temperature, "response_format":"article"}
+    if sys_prompt:
+        d2['messages'].append({"role": "system", "content": sys_prompt})
+    d2['messages'].append({"role": "user", "content": question})
+
+    for num_count in range(3):
+        try:
+           
+            response = requests.post(f'http://170.106.108.95/get_article', json=d2)
+            r_json = response.json()
+           
+            gpt_content = str(r_json)
+
+            simple_logger.info(f"问题日志:\n{question}\n回答日志:\n{gpt_content}")
+
+           
+            if not check_fucn:
+                return r_json
+
+           
+            check_result = check_fucn(str(gpt_content))
+            if check_result: 
+                return r_json
+            else:
+                raise Exception(f"第{num_count + 1}次共3次,GPT的校验没有通过,校验函数:{check_fucn.__name__}")
+
+        except Exception as e:
+            logger.info(f"小报错忽略{e}")
+        time.sleep(10)
+
+    logger.critical("get_answer_from_gpt 严重错误,3次后都失败了")
 
 
 def parse_gpt_phon_to_tuplelist(text:str) -> list:
@@ -109,4 +143,21 @@ def parse_gpt_phon_to_tuplelist(text:str) -> list:
 if __name__ == '__main__':
 
     question = "hello"
-    print(get_answer_from_gpt(question,temperature=0.8))
+   
+
+    sys_prompt = "你是一个专业的英语老师,擅长根据用户提供的词汇生成对应的英语文章和中文翻译和4个配套选择题。"
+    q= """下面我会为你提供两组数据,[单词组1]和[单词组2](里面包含词义id,英语单词,中文词义),优先使用[单词组1]内的单词,请根据这些单词的中文词义,生成一篇带中文翻译的考场英语文章,英语文章和中文翻译要有[标题]。注意这个单词有多个词义时,生成的英语文章一定要用提供的中文词义。并挑选一句复杂的句子和其中文翻译,放入difficultSentences。英语文章,放入"englishArticle"中。中文翻译,放入"chineseArticle"中。最终文中使用到的单词id放入"usedMeanIds"中。4个选择题,放入questions字段。questions结构下有4个选择题对象,其中trunk是[英语]问题文本,analysis是[中文]的问题分析,candidates是4个ABCD选项,内部有label是指选项序号A B C D ,text是[英语]选项文本,isRight是否正确答案1是正确0是错误。
+
+要求:
+1.必须用提供的这个词义的单词,其他单词使用常见、高中难度的的单词。文章整体难度适中,大约和中国的高中生,中国CET-6,雅思6分这样的难度标准。
+2.优先保证文章语句通顺,意思不要太生硬。不要为了使用特定的单词,造成文章语义前后不搭,允许不使用个别词义。
+3.文章中使用提供单词,一定要和提供单词的中文词义匹配,尤其是一词多义时,务必使用提供单词的词义。必须要用提供单词的词义。如果用到的词义与提供单词词义不一致,请不要使用这个单词。
+4.生成的文章要求600词左右,可以用\\n\\n字符分段,一般5个段落左右。第一段是文章标题。
+5.生成文章优先使用[单词组1]的词义,其次可以挑选使用[单词组2]的词义。允许不使用[单词组1]的个别单词,优先保证文章整体意思通顺连贯和故事完整。
+
+提供[单词组1]:4238 penalty:惩罚, 刑罚;4591 bare:赤裸的, 无遮蔽的;4227 stable:畜舍, 马厩;4236 psychology:心理学;4245 offense:进攻, 攻势, 冒犯, 触怒, 过错;4237 innocent:清白的, 无辜的, 天真的;4228 refrigerator:冰箱, 冷库;4247 tissue:(动植物)组织;4250 awareness:察觉, 觉悟, 意识;4234 mode:方式, 模式;4224 neat:整洁, 利索;4225 statistics:统计;4251 random:任意的, 随机的;4201 laundry:洗衣房;4545 barrel:桶, 一桶之量;4249 recruit:招募, 新成员;4229 pregnant:怀孕的, 孕育的;4235 relevant:有关的, 相关联的;4252 incentive:刺激, 激励, 鼓励;4194 grave:坟墓, 墓穴;
+提供[单词组2]:;
+"""
+    resp = get_article_gpt_pydantic(question=q,temperature=0.9,sys_prompt=sys_prompt,model="gpt-4.1")
+    print(type(resp))
+    print(resp)

+ 6 - 5
gpt/get_article2.py

@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from gpt.chatgpt import get_answer_from_gpt
+from gpt.chatgpt import get_answer_from_gpt,get_article_gpt_pydantic
 from gpt.gpt_check import CheckGptAnswer, CheckArticleResult
 from tools.new_mysql import MySQLUploader
 from tools.loglog import logger, log_err_e
@@ -189,7 +189,7 @@ class GetArticle:
         extend_words_meaning_str = ";".join([str(i['meaning_id']) + ' ' + i["spell"] + ":" + i["meaning"] for i in extend_words])
 
         no_escape_code = r"\\n\\n"
-        json_model = r'{"difficultSentences":[{"english":"string","chinese":"string"}],"usedMeanIds":[0,0,0],"englishArticle":"string","chineseArticle":"string","questions":[{"trunk":"string","analysis":"string","candidates":[{"label":"string","text":"string","isRight":0}]}]}'
+       
         sys_prompt = "你是一个专业的英语老师,擅长根据用户提供的词汇生成对应的英语文章和中文翻译和4个配套选择题。"
         q = f"""下面我会为你提供两组数据,[单词组1]和[单词组2](里面包含词义id,英语单词,中文词义),优先使用[单词组1]内的单词,请根据这些单词的中文词义,\
 生成一篇带中文翻译的考场英语文章,英语文章和中文翻译要有[标题]。注意这个单词有多个词义时,生成的英语文章一定要用提供的中文词义。并挑选一句复杂的句子和其中文翻译,放入difficultSentences。\
@@ -202,7 +202,6 @@ class GetArticle:
 3.文章中使用提供单词,一定要和提供单词的中文词义匹配,尤其是一词多义时,务必使用提供单词的词义。必须要用提供单词的词义。如果用到的词义与提供单词词义不一致,请不要使用这个单词。
 4.生成的文章要求{select_word_count}词左右,可以用{no_escape_code}字符分段,一般{select_paragraph_count}个段落左右。第一段是文章标题。
 5.生成文章优先使用[单词组1]的词义,其次可以挑选使用[单词组2]的词义。允许不使用[单词组1]的个别单词,优先保证文章整体意思通顺连贯和故事完整。
-6.回复紧凑无空格的json数据,示例:{json_model}
 
 提供[单词组1]:{core_words_meaning_str};
 提供[单词组2]:{extend_words_meaning_str};
@@ -210,9 +209,11 @@ class GetArticle:
         try:
             real_ip = self.real_ip_dict[task_id]
             demo_name = self.demo_name[task_id]
-            r_json = json.loads(get_answer_from_gpt(q, temperature=1, json_resp=True, real_ip=real_ip, demo_name=demo_name, model='gpt-4.1',
-                                                    check_fucn=CheckArticleResult.get_article_1, max_tokens=8000, sys_prompt=sys_prompt))
+           
+           
 
+            r_json = json.loads(get_article_gpt_pydantic(q, temperature=0.9, real_ip=real_ip, demo_name=demo_name, model='gpt-4.1',
+                                                 max_tokens=4000, sys_prompt=sys_prompt))
            
             allWordAmount = 0
             allWordAmount += len(split_text_to_word(r_json["englishArticle"]))

+ 2 - 2
mock/mock_request.py

@@ -123,7 +123,7 @@ def get_article2_1():
                                   {'spell': 'waste', 'meaning': '浪费, 荒芜, 废物', 'word_id': 1160701, 'meaning_id': 1292},
                                   {'spell': 'environment', 'meaning': '环境, 外界', 'word_id': 873514, 'meaning_id': 1293},
                                   {'spell': 'memory', 'meaning': '记忆, 记忆力, 回忆', 'word_id': 981104, 'meaning_id': 1294}],
-                 'take_count': 1, 'student_stage': 3, 'demo_name': '春笋英语'}
+                 'take_count': 1, 'student_stage': 3, 'demo_name': '春笋英语',"article_difficulty":1500}
 
     r = requests.post(f"{use_address}/article/reading-comprehension", json=json_data)
     r_json = r.json()
@@ -199,6 +199,6 @@ if __name__ == '__main__':
 
 
    
+    print(get_article2_1())
    
-    download_word()