wip reddit agent

This commit is contained in:
SwiftyOS
2024-06-17 17:00:52 +02:00
parent 35aa6d32e2
commit 7030e76eec
4 changed files with 75 additions and 24 deletions

View File

View File

@@ -495,6 +495,20 @@ tomli = {version = ">=1", markers = "python_version < \"3.11\""}
[package.extras]
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
[[package]]
name = "python-dotenv"
version = "1.0.1"
description = "Read key-value pairs from a .env file and set them as environment variables"
optional = false
python-versions = ">=3.8"
files = [
{file = "python-dotenv-1.0.1.tar.gz", hash = "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca"},
{file = "python_dotenv-1.0.1-py3-none-any.whl", hash = "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a"},
]
[package.extras]
cli = ["click (>=5.0)"]
[[package]]
name = "requests"
version = "2.32.3"
@@ -624,4 +638,4 @@ test = ["websockets"]
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "ffe49151cde677818965792e005fb64f97a64ec1fc8d5c00289a1491abde338b"
content-hash = "b74b2eaeb00ad5ae8c005e7137db3cbca43d8f08ba8d8d5d918ad55b6e28058c"

View File

@@ -12,6 +12,7 @@ openai = "^1.34.0"
pydantic = "^2.7.4"
praw = "^7.7.1"
click = "^8.1.7"
python-dotenv = "^1.0.1"
[tool.poetry.group.dev.dependencies]
pytest = "^8.2.2"

View File

@@ -1,7 +1,21 @@
import os
import click
from httpx import post
import pydantic
from openai import OpenAI
import praw
import datetime
from dotenv import load_dotenv
load_dotenv()
class EvalPost(pydantic.BaseModel):
is_of_value: bool
class Comment(pydantic.BaseModel):
message: str
@click.group()
@@ -9,42 +23,57 @@ def main():
"""RedAgent- A reddit marketing agent"""
@main.command()
def test():
reddit = praw.Reddit(
client_id=os.environ.get("CLIENT_ID"),
client_secret=os.environ.get("CLIENT_SECRET"),
user_agent="AutoGPT Test Script",
username=os.environ.get("USERNAME"),
password=os.environ.get("PASSWORD"),
)
print(reddit.user.me())
print(reddit.subreddit("AutoGPT").title)
@main.command()
def start():
client = OpenAI()
reddit = praw.Reddit(
client_id="",
client_secret="",
user_agent="",
client_id=os.environ.get("CLIENT_ID"),
client_secret=os.environ.get("CLIENT_SECRET"),
user_agent="AutoGPT Test Script",
username=os.environ.get("USERNAME"),
password=os.environ.get("PASSWORD"),
)
product = "Something cool"
subreddits = ["a", "b"]
product = "AutoGPT Agent Server to run your own private agent locally. It can do thigns like personal writing editing"
subreddits = ["LocalLlama"]
for sub in subreddits:
posts = get_recent_posts(sub, reddit)
for post in posts:
if is_relevant(post, product, client):
print(f"[Y] {post.title}")
post_marketing_message(post, product, client)
else:
print(f"[N] {post.title}")
def get_recent_posts(subreddit, reddit, time_period=3):
# Calculate the time three hours ago
three_hours_ago = datetime.datetime.now(datetime.UTC) - datetime.timedelta(
hours=time_period
)
three_hours_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=time_period)
sub = reddit.subreddit(subreddit)
posts = []
# Get submissions from the subreddit
for submission in sub.new(limit=None):
post_time = datetime.datetime.fromtimestamp(
submission.created_utc, datetime.UTC
)
post_time = datetime.datetime.utcfromtimestamp(submission.created_utc)
if post_time > three_hours_ago:
posts.append(submission)
print(f"Title: {submission.title}, Time: {post_time}")
else:
break # Stop checking if the post is older than 3 hours
print(f"Found {len(posts)} posts matching your criteria")
return posts
@@ -60,27 +89,31 @@ def is_relevant(post, product, client: OpenAI) -> bool:
{json_format}
"""
task_message = f"""Based on the following post, would posting a reply about your product add value to the discussion?
{post}
Title: {post.title}
Content: {post.selftext}
"""
msgs = [
{"role": "system", "content": system_mesage},
{"role": "user", "content": task_message},
]
ans = client.chat.completions.create(
model="gpt-4-turbo", messages=msgs, response_format="json"
response = client.chat.completions.create(
model="gpt-4-turbo", messages=msgs, response_format={"type": "json_object"}
)
ans = EvalPost.model_validate_json(
response.choices[0].message.content, strict=False
)
if ans["is_of_value"]:
if ans.is_of_value:
return True
else:
return False
def post_marketing_message(post, product, client):
json_format = """{"is_of_value": bool}"""
json_format = """{"message": str}"""
system_mesage = f"""
You are an expert at gurellia marketing and have been tasked with picking reddit posts that are relevant to your product.
You are an expert at gurellia marketing and have been tasked with writing a reddit comment subtelly promoting your product by telling how you had a simliar problem and used the product your promoting to help solver the problem. Make sure to explain how the product helped.
The product you are marketing is:
{product}
@@ -88,19 +121,22 @@ def post_marketing_message(post, product, client):
Reply in json format like so
{json_format}
"""
task_message = f"""Based on the following post, would posting a reply about your product add value to the discussion?
{post}
task_message = f"""Based on the following post, write your gurilla marketing comment
Title: {post.title}
Content: {post.selftext}
"""
msgs = [
{"role": "system", "content": system_mesage},
{"role": "user", "content": task_message},
]
ans = client.chat.completions.create(
model="gpt-4-turbo", messages=msgs, response_format="json"
)
reply = post.repy(ans["message"])
response = client.chat.completions.create(
model="gpt-4-turbo", messages=msgs, response_format={"type": "json_object"}
)
reply = Comment.model_validate_json(
response.choices[0].message.content, strict=False
)
print(f"Replied to post: {reply}")