稍微扩展一下这个想法,让用户能够使用自然语言与应用程序进行交互,这几乎是我对AI的梦想。通过Discourse和Data Explorer插件,可以实现诸如“返回对版主感兴趣的用户列表”之类的提示。甚至更好,“向社区版主群组发送邀请给所有对版主感兴趣的用户。”
回到现在可能实现的功能:
通过测试ChatGPT 3.0和3.5,我在要求它返回PostgreSQL查询并将我感兴趣的表结构复制到提示中时得到了很好的结果。例如:
摘要
我: 返回一个PostgreSQL查询,列出在以下数据库结构中属于“community_moderators”群组的所有用户的用户名:
# == Schema Information
#
# Table name: groups
#
# id :integer not null, primary key
# name :string not null
# == Schema Information
#
# Table name: group_users
#
# id :integer not null, primary key
# group_id :integer not null
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# username :string(60) not null
ChatGPT 3.5: 当然,这是一个PostgreSQL查询,它根据给定的数据库结构列出属于“community_moderators”群组的所有用户的用户名:
SELECT u.username
FROM users u
JOIN group_users gu ON u.id = gu.user_id
JOIN groups g ON gu.group_id = g.id
WHERE g.name = 'community_moderators';
此查询通过将users表与group_users表在用户ID上连接,然后与groups表在群组ID上连接来检索users表中的用户名。它过滤结果,只包括属于“community_moderators”群组的用户的用户名。
我: 这是应用程序的topics和posts表的结构:
# == Schema Information
#
# Table name: topics
#
# id :integer not null, primary key
# title :string not null
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# user_id :integer
# topic_id :integer not null
# post_number :integer not null
# raw :text not null
返回一个查询,列出所有由“community_moderators”群组的成员创建的帖子的所有主题ID、帖子ID和用户名,这些帖子表明发帖人对如何管理极化讨论感兴趣。
ChatGPT 3.5: 要实现这一点,您需要根据提供的模式连接posts、topics、users和group_users表。这是一个检索所需信息的PostgreSQL查询:
SELECT t.id AS topic_id, p.id AS post_id, u.username
FROM posts p
JOIN topics t ON p.topic_id = t.id
JOIN users u ON p.user_id = u.id
JOIN group_users gu ON u.id = gu.user_id
JOIN groups g ON gu.group_id = g.id
WHERE g.name = 'community_moderators'
AND (p.raw ILIKE '%moderate%' OR p.raw ILIKE '%polarizing%' OR p.raw ILIKE '%discussion%');
此查询检索由“community_moderators”群组的成员创建的、包含指示对管理极化讨论感兴趣的特定关键字的帖子的topic_id、post_id和username。ILIKE运算符用于posts表raw列中的不区分大小写的模式匹配。
请注意,此查询假定您提供的关键字指示了所需的帖子。您可能需要根据您的具体标准调整或扩展关键字。
我: 编写一个查询,返回自2022年9月以来,由社区版主群组的成员创建的、表明对如何管理极化讨论感兴趣的帖子的每周发布数量。
ChatGPT 3.5: 要实现这一点,您可以使用以下PostgreSQL查询来返回自2022年9月以来,由“community_moderators”群组的成员创建的、表明对如何管理极化讨论感兴趣的帖子的每周发布数量:
SELECT
DATE_TRUNC('week', p.created_at) AS week_start,
COUNT(p.id) AS post_count
FROM
posts p
JOIN
users u ON p.user_id = u.id
JOIN
group_users gu ON u.id = gu.user_id
JOIN
groups g ON gu.group_id = g.id
WHERE
g.name = 'community_moderators'
AND (p.raw ILIKE '%moderate%'
OR p.raw ILIKE '%polarizing%'
OR p.raw ILIKE '%political%'
OR p.raw ILIKE '%contentious%'
OR p.raw ILIKE '%debate%'
OR p.raw ILIKE '%controversial%'
OR p.raw ILIKE '%discussion%')
AND p.created_at >= '2022-09-01' -- Start date: September 2022
GROUP BY
week_start
ORDER BY
week_start;
此查询计算自2022年9月以来,由“community_moderators”群组的成员创建的、包含指定关键字的帖子的数量,按每周分组。它按周对结果进行分组,并按时间顺序排序。请确保根据您的实际数据库模式调整列名和表关系。
在我看来,这些结果很好,并且代表了我过去花费大量时间编写的查询类型。我假设可以对Discourse数据库结构进行模型训练,这样就可以在提示中省略有关结构的详细信息。