CodeLlama 70B 对比 34B 对比 Mistral-medium 对比 GPT-4

全新发布的CodeLlama 70B是否令人惊叹,不仅在HumanEval数值方面,而且在实际任务中也表现出色?

测试提示:

修改以下代码,为DataFileHandler的结构定义一个接口DataHandler。创建一个新的类DataSQLHandler来实现DataHandler接口,并使用SQLite来实现DataFileHandler类似的功能。

import pandas as pd
import os
import signal
from datetime import datetime
from email.utils import formatdate
import sys

class DataFileHandler:
def __init__(self, main_key, site, query):
self.main_key = main_key
self.site = site
self.run_start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.main_csv_file = f'{self.site}-{query}.csv'
self.temp_csv_file = f'{self.site}-{self.run_start_time}.csv'
self.last_saved_main_key = self.get_last_saved_key()
print_err(f"last_saved_main_key={self.last_saved_main_key}")
signal.signal(signal.SIGINT, self.save_main_file)

def save_main_file(self, signum=None, frame=None):#signal handler(signum=None, frame=None)
if os.path.exists(self.temp_csv_file):
temp_df = pd.read_csv(self.temp_csv_file)
if not temp_df.empty:
reversed_df = temp_df.iloc[::-1]
if os.path.exists(self.main_csv_file):
reversed_df.to_csv(self.main_csv_file, mode='a', index=False, header=False)
else:
reversed_df.to_csv(self.main_csv_file, mode='w', index=False, header=True)
os.remove(self.temp_csv_file)

def append_data(self, data):
data['record_time'] = formatdate(localtime=True, usegmt=True)
df = pd.DataFrame([data])
if not os.path.exists(self.temp_csv_file):
df.to_csv(self.temp_csv_file, mode='w', index=False, header=True)
else:
df.to_csv(self.temp_csv_file, mode='a', index=False, header=False)

def get_last_saved_key(self):
if os.path.exists(self.main_csv_file):
df = pd.read_csv(self.main_csv_file)
if not df.empty:
return df.iloc[-1][self.main_key]
return None

def is_duplicate(self, current_data_key):
return current_data_key == self.last_saved_main_key

GPT-4是用中文提示(意思相同,措辞稍有差异)的,并且代码几乎完美。我不打算在这里包含它的说明文本。

import sqlite3

from abc import ABC, abstractmethod

class DataHandler(ABC):

@abstractmethod
def save_main_file(self, signum=None, frame=None):
pass

@abstractmethod
def append_data(self, data):
pass

@abstractmethod
def get_last_saved_key(self):
pass

@abstractmethod
def is_duplicate(self, current_data_key):
pass

# DataFileHandler 现在继承自 DataHandler,并实现其方法
class DataFileHandler(DataHandler):
# [原有的 DataFileHandler 类的实现]
...

class DataSQLHandler(DataHandler):
def __init__(self, main_key, site, query, db_file='data.db'):
self.main_key = main_key
self.site = site
self.db_file = db_file
self.table_name = f'{site}_{query}'
self.create_table()
self.last_saved_main_key = self.get_last_saved_key()

def create_table(self):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(f'''CREATE TABLE IF NOT EXISTS {self.table_name}
(record_time TEXT, {self.main_key} TEXT, PRIMARY KEY({self.main_key}))''')
conn.commit()
conn.close()

def save_main_file(self, signum=None, frame=None):
# 在SQLite中,此方法可能不需要,因为数据已经在数据库中(this means: In SQLite this method may not be needed as the data is already in the database)
pass

def append_data(self, data):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
columns = ', '.join(data.keys())
placeholders = ':'+', :'.join(data.keys())
c.execute(f'INSERT OR IGNORE INTO {self.table_name} ({columns}) VALUES ({placeholders})', data)
conn.commit()
conn.close()

def get_last_saved_key(self):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(f'SELECT {self.main_key} FROM {self.table_name} ORDER BY ROWID DESC LIMIT 1')
result = c.fetchone()
conn.close()
return result[0] if result else None

def is_duplicate(self, current_data_key):
return current_data_key == self.last_saved_main_key

Mistral-medium(由perplexity托管)与GPT-4处于同一水平!

以下是添加所要求更改的修改后代码:

import sqlite3
import pandas as pd
import os
import signal
from datetime import datetime
from email.utils import formatdate
import sys

class DataHandler:
def append_data(self, data):
pass

def get_last_saved_key(self):
pass

def is_duplicate(self, current_data_key):
pass

class DataFileHandler(DataHandler):
#identical copy of the codein prompt
#...

class DataSQLHandler(DataHandler):
def __init__(self, main_key, site, query):
self.main_key = main_key
self.site = site
self.run_start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.conn = sqlite3.connect(f'{self.site}-{query}.db')
self.cursor = self.conn.cursor()
self.cursor.execute('''CREATE TABLE IF NOT EXISTS data
(record_time TEXT, {main_key} TEXT)'''.format(main_key=self.main_key))
self.last_saved_main_key = self.get_last_saved_key()
print_err(f"last_saved_main_key={self.last_saved_main_key}")
signal.signal(signal.SIGINT, self.save_main_file)

def save_main_file(self, signum=None, frame=None):
pass

def append_data(self, data):
data['record_time'] = formatdate(localtime=True, usegmt=True)
self.cursor.execute('''INSERT INTO data VALUES (?, ?)''',
(data['record_time'], data[self.main_key]))
self.conn.commit()

def get_last_saved_key(self):
self.cursor.execute('''SELECT {main_key} FROM data ORDER BY record_time DESC LIMIT 1'''.format(main_key=self.main_key))
result = self.cursor.fetchone()
if result:
return result[0]
return None

def is_duplicate(self, current_data_key):
self.cursor.execute('''SELECT COUNT(*) FROM data WHERE {main_key}=? ORDER BY record_time DESC LIMIT 1'''.format(main_key=self.main_key),
(current_data_key,))
count = self.cursor.fetchone()[0]
return count > 0

在上面的代码中,我定义了一个名为`DataHandler`的接口,其中包含三个抽象方法:`append_data`、`get_last_saved_key`和`is_duplicate`。然后,我修改了原始的`DataFileHandler`类以实现此接口,并添加了一个名为`DataSQLiteHandler`的新类,该类也实现了`DataHandler`接口,并使用SQLite来存储数据,而不是使用CSV文件。

请注意,在`DataSQLiteHandler`类中不需要`save_main_file`方法,因为SQLite会自动处理数据持久化。此外,`DataSQLiteHandler`类中的`is_duplicate`方法使用SQL查询来检查当前数据键是否已存在于数据库中。

我对CodeLlama-70B-instruct感到失望。

1. 为DataFileHandler的结构定义一个接口DataHandler。

class DataHandler:
def __init__(self, main_key, site, query):
pass

def save_main_file(self):
pass

def append_data(self, data):
pass

def get_last_saved_key(self):
pass

def is_duplicate(self, current_data_key):
pass

2. 创建一个新的类 DataSQLHandler 来实现 DataHandler 接口,并使用 SQLite 来实现 DataFileHandler 的类似功能。

import sqlite3

class DataSQLHandler(DataHandler):
def __init__(self, main_key, site, query):
self.main_key = main_key
self.site = site
self.run_start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.main_table = f'{self.site}-{query}'
self.temp_table = f'{self.site}-{self.run_start_time}'
self.last_saved_main_key = self.get_last_saved_key()
print_err(f"last_saved_main_key={self.last_saved_main_key}")
signal.signal(signal.SIGINT, self.save_main_file)

def save_main_file(self, signum=None, frame=None):#signal handler(signum=None, frame=None)
if self.temp_table_exists():
temp_df = self.get_temp_table_data()
if not temp_df.empty:
reversed_df = temp_df.iloc[::-1]
if self.main_table_exists():
self.append_to_main_table(reversed_df)
else:
self.create_main_table(reversed_df)
self.drop_temp_table()

def append_data(self, data):
data['record_time'] = formatdate(localtime=True, usegmt=True)
df = pd.DataFrame([data])
if not self.temp_table_exists():
self.create_temp_table(df)
else:
self.append_to_temp_table(df)

def get_last_saved_key(self):
if self.main_table_exists():
df = self.get_main_table_data()
if not df.empty:
return df.iloc[-1][self.main_key]
return None

def is_duplicate(self, current_data_key):
return current_data_key == self.last_saved_main_key

def temp_table_exists(self):
pass

def main_table_exists(self):
pass

def get_temp_table_data(self):
pass

def get_main_table_data(self):
pass

def append_to_temp_table(self, df):
pass

def append_to_main_table(self, df):
pass

def create_temp_table(self, df):
pass

def create_main_table(self, df):
pass

def drop_temp_table(self):
pass

在DataSQLHandler类中,方法`temp_table_exists`,`main_table_exists`,`get_temp_table_data`,`get_main_table_data`,`append_to_temp_table`,`append_to_main_table`,`create_temp_table`,`create_main_table`和`drop_temp_table`被留空,因为它们需要特定的sqlite实现细节。

请再试一次。

1. 创建一个名为DataSQLHandler的新类来实现DataHandler接口,并使用SQLite来实现DataFileHandler的类似功能。

import sqlite3
import os
import signal
from datetime import datetime
from email.utils import formatdate
import sys

class DataSQLHandler:
def __init__(self, main_key, site, query):
self.main_key = main_key
self.site = site
self.run_start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.db_file = f'{self.site}-{query}.db'
self.table_name = f'{self.site}-{self.run_start_time}'
self.last_saved_main_key = self.get_last_saved_key()
print_err(f"last_saved_main_key={self.last_saved_main_key}")
signal.signal(signal.SIGINT, self.save_main_file)

def save_main_file(self, signum=None, frame=None):#signal handler(signum=None, frame=None)
if os.path.exists(self.db_file):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(f"SELECT * FROM {self.table_name}")
rows = c.fetchall()
if rows:
reversed_rows = rows[::-1]
if os.path.exists(self.main_csv_file):
with open(self.main_csv_file, 'a') as f:
for row in reversed_rows:
f.write(','.join(row) + '\n')
else:
with open(self.main_csv_file, 'w') as f:
for row in reversed_rows:
f.write(','.join(row) + '\n')
conn.close()

def append_data(self, data):
data['record_time'] = formatdate(localtime=True, usegmt=True)
if not os.path.exists(self.db_file):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(f"CREATE TABLE {self.table_name} (main_key text, record_time text)")
conn.commit()
conn.close()
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(f"INSERT INTO {self.table_name} VALUES (?, ?)", (data['main_key'], data['record_time']))
conn.commit()
conn.close()

def get_last_saved_key(self):
if os.path.exists(self.db_file):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(f"SELECT * FROM {self.table_name}")
rows = c.fetchall()
if rows:
return rows[-1][0]
return None

新班级看起来更好,但它忘记了定义一个接口的第一个任务。

所以我也尝试了他的小弟弟CodeLlama-34B-instruct。

为`DataFileHandler`的结构创建接口`DataHandler`,您可以按照以下方式定义接口:

from abc import ABC, abstractmethod

class DataHandler(ABC):
@abstractmethod
def save_main_file(self, signum=None, frame=None):
pass

@abstractmethod
def append_data(self, data):
pass

@abstractmethod
def get_last_saved_key(self):
pass

@abstractmethod
def is_duplicate(self, current_data_key):
pass

这个接口定义了四个在`DataFileHandler`和`DataSQLHandler`中都通用的方法:`save_main_file`,`append_data`,`get_last_saved_key`和`is_duplicate`。这些方法被标记为抽象,意味着它们必须由实现`DataHandler`接口的任何类来实现。

为了创建一个新的类`DataSQLHandler`,该类实现`DataHandler`接口并使用SQLite来实现`DataFileHandler`的类似功能,您可以将该类定义如下:

import sqlite3

class DataSQLHandler(DataHandler):
def __init__(self, main_key, site, query):
self.main_key = main_key
self.site = site
self.run_start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.main_db_file = f'{self.site}-{query}.db'
self.temp_db_file = f'{self.site}-{self.run_start_time}.db'
self.last_saved_main_key = self.get_last_saved_key()
print_err(f"last_saved_main_key={self.last_saved_main_key}")
signal.signal(signal.SIGINT, self.save_main_file)

def save_main_file(self, signum=None, frame=None):
if os.path.exists(self.temp_db_file):
temp_db = sqlite3.connect(self.temp_db_file)
temp_db.commit()
temp_db.close()
os.remove(self.temp_db_file)

def append_data(self, data):
data['record_time'] = formatdate(localtime=True, usegmt=True)
db = sqlite3.connect(self.main_db_file)
db.execute("INSERT INTO data (record_time, data) VALUES (?, ?)", (data['record_time'], data))
db.commit()
db.close()

def get_last_saved_key(self):
db = sqlite3.connect(self.main_db_file)
cursor = db.execute("SELECT * FROM data ORDER BY record_time DESC LIMIT 1")
last_row = cursor.fetchone()
db.close()
if last_row:
return last_row[0]
return None

def is_duplicate(self, current_data_key):
db = sqlite3.connect(self.main_db_file)
cursor = db.execute("SELECT * FROM data WHERE record_time = ?", (current_data_key,))
result = cursor.fetchone()
db.close()
return result is not None

该类与`DataFileHandler`定义了相同的方法,但是使用SQLite来存储数据,而不是CSV文件。`save_main_file`方法将更改提交到临时数据库文件,然后删除该文件。`append_data`方法将数据插入到主数据库文件中。`get_last_saved_key`方法从主数据库文件中检索最后保存的键。`is_duplicate`方法检查当前数据键是否已存在于主数据库文件中。

要使用`DataSQLHandler`类,您可以创建一个实例,并以与`DataFileHandler`相同的方式调用其方法。

data_handler = DataSQLHandler('main_key', 'site', 'query')
data_handler.append_data({'key': 'value'})
data_handler.save_main_file()

这将创建一个新的SQLite数据库文件,并将数据追加到其中,然后将更改保存到文件中。

这似乎比70B还要好!

我对Mistral-Medium目前的大小更感兴趣。

2024-01-31 04:23:12 AI中文站翻译自原文