Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finetuning rest #264

Merged
merged 77 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
c5bcb20
finetuning commenting prompt
antopiahk Apr 15, 2024
cc0ae9d
fine tuning the bot and adding uniqueness checking in db.py
antopiahk Apr 15, 2024
b91fb13
fixing submissions and leads not getting submitted to db
antopiahk Apr 20, 2024
9f61585
fixed db checking for existing leads
antopiahk Apr 29, 2024
d80539d
nix shell removing poetry
antopiahk May 3, 2024
93d9e3f
convert rest to using new organizational fastapi structure
eksno May 3, 2024
ff36f79
whops forgor router
eksno May 3, 2024
f66880f
adding option for adding instructions to generate comment and improvi…
antopiahk May 15, 2024
1a46729
Move api keys to own router, add small fixups and error handling (#194)
failandimprove1 Apr 16, 2024
f4a2dd1
Add more tools (#195)
LVGrinder Apr 17, 2024
8a4f43f
Add subscription endpoints (#196)
LVGrinder Apr 17, 2024
3699021
Fix the types of session and message models for the openapi code gene…
failandimprove1 Apr 17, 2024
7f844a9
Gitignore cache and change api keys path to kebabcase (#199)
failandimprove1 Apr 17, 2024
205c016
Add billing endpoints (#198)
LVGrinder Apr 17, 2024
08026ce
Fix error output on inputting session (#202)
failandimprove1 Apr 17, 2024
787ecd6
Fix error handling on sessions (#203)
failandimprove1 Apr 17, 2024
40f94de
Add tiers endpoints (#200)
LVGrinder Apr 17, 2024
b3f993f
Add one parenthesis (plus some empty lines) (#204)
failandimprove1 Apr 17, 2024
51c83b4
Fix code smell complaints (#205)
failandimprove1 Apr 17, 2024
4d02cbd
Add tool endpoints (#201)
failandimprove1 Apr 17, 2024
0653dae
Fix sonarcloud issues (#206)
failandimprove1 Apr 17, 2024
d106a77
Format and fix typing (#207)
failandimprove1 Apr 17, 2024
3dc7a54
Add prompt type and fix typing of edges (#209)
failandimprove1 Apr 18, 2024
f477849
Mostly implement OpenAPI fetch (#208)
eksno Apr 18, 2024
9eeafab
Add new agent model implementation (#210)
failandimprove1 Apr 18, 2024
3da6742
Bump the pip group across 2 directories with 1 update (#211)
dependabot[bot] Apr 18, 2024
832bfd3
Add validate crews endpoint (#212)
LVGrinder Apr 19, 2024
02a0620
Change path params to id and fixup validation (#213)
failandimprove1 Apr 19, 2024
d985940
Add endpoint for getting all models (#214)
failandimprove1 Apr 19, 2024
77c76a7
Agent and Crew Library (#215)
eksno Apr 21, 2024
3d802f9
Crews mostly working + some agent stuff W.I.P (#216)
eksno Apr 22, 2024
b3b4f11
Fixup API Agent (#217)
eksno Apr 22, 2024
ffecf48
Change/update agent models, removed models endpoint (#218)
failandimprove1 Apr 22, 2024
87ecc04
Fix up the crew models to follow db changes (#219)
failandimprove1 Apr 22, 2024
1c6a501
Add new fields and rename fields in mock (#220)
failandimprove1 Apr 22, 2024
89a59fb
Change api key types to provider (#221)
LVGrinder Apr 22, 2024
db03381
Change api key type to api provider in tool models and endpoints (als…
failandimprove1 Apr 22, 2024
fd372a5
Agent Web CRUD Implemented (#223)
eksno Apr 22, 2024
f304fa8
Last Crews and Agents before sessions (#224)
eksno Apr 23, 2024
8549c4d
Fix bug with empty tool list (#225)
failandimprove1 Apr 23, 2024
cc0c398
Change how description is given to agent (#226)
failandimprove1 Apr 23, 2024
16bb1b8
loading sessions working (#227)
eksno Apr 23, 2024
0a9afdb
Add logging config file, change file structure (#228)
failandimprove1 Apr 23, 2024
a06da7a
Default agent list on creating crew (#229)
failandimprove1 Apr 23, 2024
cb46dde
"Fix" tools in mock (#230)
failandimprove1 Apr 23, 2024
6d602f9
Mount subapi onto the fastapi app (#232)
failandimprove1 Apr 23, 2024
bd676a2
Add lib folder (#233)
LVGrinder Apr 24, 2024
522d3c7
Formatting, removing unused imports, fix imports with new lib directo…
failandimprove1 Apr 24, 2024
9721da0
Sessions working completely! (but not too well 😓) (#235)
eksno Apr 24, 2024
ce96699
Add funding system for running crews (#236)
failandimprove1 Apr 27, 2024
38bcb62
Auth Fixup (#237)
eksno Apr 28, 2024
09254aa
Hotfix Broken Package Lucide (#238)
eksno Apr 28, 2024
43c96b4
Fix Crew editing, authentication, openapi types, and some more (#240)
eksno Apr 29, 2024
92db481
Add stopping crew runs when the given profile has no funds (#241)
failandimprove1 Apr 29, 2024
bb41c21
Add slightly working rag agents, seemingly fixed sessions stopping un…
failandimprove1 Apr 30, 2024
49d30d1
Add new cost calculation that uses agent costs properly (#243)
failandimprove1 Apr 30, 2024
69fbd9a
feat: Update authentication and session handling (#244)
eksno May 2, 2024
af38ec9
feat: Update dependencies and improve authentication flow (#245)
eksno May 3, 2024
a7c7649
Bump tqdm in /apps/api in the pip group across 1 directory (#246)
dependabot[bot] May 3, 2024
be84507
Bump the pip group across 2 directories with 1 update (#247)
dependabot[bot] May 3, 2024
1dea6ca
feat: Use environment variable for redirect url (#248)
eksno May 4, 2024
100e878
fix: finalize the crew editor for first launch and add email to profi…
eksno May 4, 2024
df332bc
fix: agent creation and editor (#250)
eksno May 4, 2024
c88b262
fix: add error handling for crew saving operation (#251)
eksno May 4, 2024
9c7a2c3
feat(api): use PORT from environment variables (#252)
failandimprove1 May 6, 2024
efb685b
Change formatter to not use poetry (#253)
failandimprove1 May 6, 2024
d4044f2
fix: set moch receiver_id as it's faultily required by api (#254)
eksno May 7, 2024
22e5007
Fixup prompt improver, add funding dependency (#255)
failandimprove1 May 7, 2024
bef1d36
add gpt-4 (also added azure keys in oai config list) (#256)
failandimprove1 May 7, 2024
876931c
make a single field nullable (#257)
failandimprove1 May 7, 2024
8f50431
fix: mostly typos, but also add support for daisyui (#258)
eksno May 8, 2024
17ce075
feat: auto-save (#260)
eksno May 9, 2024
a2a80db
feat(rest): change llm models to use azure openai keys (#259)
failandimprove1 May 9, 2024
f1f22ed
fix: remove error toast on agent deletion (#261)
eksno May 9, 2024
c25c288
fix(agent-model): rename 'gpt-3.5-turbo' to 'gpt-35-turbo' (#262)
eksno May 9, 2024
e52201d
feat(library-create-button): add ScrollArea to Dialog.Content (#263)
eksno May 9, 2024
feaf7fb
Merge branch 'alpha' into feature/finetuning-rest
antopiahk May 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/rest/api/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s
22 changes: 22 additions & 0 deletions apps/rest/api/logging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 1
disable_existing_loggers: false

formatters:
standard:
format: "%(asctime)s - %(levelname)s - %(message)s"

handlers:
console:
class: logging.StreamHandler
formatter: standard
stream: ext://sys.stdout

loggers:
uvicorn:
error:
propagate: true

root:
level: INFO
handlers: [console]
propagate: no
2,768 changes: 0 additions & 2,768 deletions apps/rest/api/poetry.lock

This file was deleted.

1 change: 1 addition & 0 deletions apps/rest/api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mail = "^2.1.0"
python-dotenv = "^1.0.1"
diskcache = "^5.6.3"
pydantic = "^2.6.4"
python-upwork-oauth2 = "^3.1.0"

[tool.poetry.group.dev.dependencies]
mypy = "^1.7.0"
Expand Down
1 change: 0 additions & 1 deletion apps/rest/api/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
name = "pipzone";
targetPkgs = pkgs: (with pkgs; [
python312
poetry
]);
runScript = "fish";
}).env
175 changes: 35 additions & 140 deletions apps/rest/api/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,148 +1,43 @@
import os
from dotenv import load_dotenv
import logging
import diskcache as dc
import threading
from uuid import uuid4
import uvicorn

from .saving import update_db_with_submission
from . import mail
from .reddit_utils import get_subreddits
from .relevance_bot import evaluate_relevance
from .interfaces import db
from . import comment_bot
from .models import (
PublishCommentRequest,
GenerateCommentRequest,
FalseLead,
)
from .reddit_worker import RedditStreamWorker

from fastapi import FastAPI, HTTPException
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse


# Relevant subreddits to Startino
SUBREDDIT_NAMES = (
"SaaS+SaaSy+startups+YoungEntrepreneurs+NoCodeSaas+nocode+cofounder+Entrepreneur"
)

load_dotenv()
REDDIT_PASSWORD = os.getenv("REDDIT_PASSWORD")
REDDIT_USERNAME = os.getenv("REDDIT_USERNAME")

if REDDIT_PASSWORD is None:
raise ValueError("REDDIT_PASSWORD is not set")

if REDDIT_USERNAME is None:
raise ValueError("REDDIT_USERNAME is not set")

logger = logging.getLogger("root")

app = FastAPI()
workers = {}

app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173",
"http://localhost:5174",
"http://localhost:8000",
"http://localhost:8001",
"http://localhost:8080",
"http://localhost:8081",
"http://127.0.0.1:5173",
"http://127.0.0.1:5174",
"http://127.0.0.1:8000",
"http://127.0.0.1:8001",
"http://127.0.0.1:8080",
"http://127.0.0.1:8081",
"https://aiti.no",
"https://api.aiti.no",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/")
def redirect_to_docs() -> RedirectResponse:
return RedirectResponse(url="/docs")


@app.get("/test")
def test():
print("TEST")


@app.post("/start")
def start_stream():
worker_id = str(uuid4())
worker = RedditStreamWorker(SUBREDDIT_NAMES, REDDIT_USERNAME, REDDIT_PASSWORD)
thread = threading.Thread(target=worker.start)
workers[worker_id] = (worker, thread)
thread.start()
return {"worker_id": worker_id}


@app.post("/stop/{worker_id}")
def stop_stream(worker_id: str):
worker, thread = workers.get(worker_id, (None, None))
if worker is None:
raise HTTPException(status_code=404, detail="Worker not found")
worker.stop()
thread.join(timeout=10) # Waits 10 seconds for the thread to finish

if thread.is_alive():
logger.error(f"Thread for Reddit worker didn't stop in time")

del workers[worker_id] # Cleanup

return {"message": "Stream stopped"}


@app.post("/generate-comment")
def generate_comment(generate_request: GenerateCommentRequest):
comment: str = comment_bot.generate_comment(
generate_request.title,
generate_request.selftext,
from src.routers import index

def create_app() -> FastAPI:
app = FastAPI()

app.include_router(index.router)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173",
"http://localhost:5174",
"http://localhost:8000",
"http://localhost:8001",
"http://localhost:8080",
"http://localhost:8081",
"http://127.0.0.1:5173",
"http://127.0.0.1:5174",
"http://127.0.0.1:8000",
"http://127.0.0.1:8001",
"http://127.0.0.1:8080",
"http://127.0.0.1:8081",
"https://rest.futi.no",
"https://api.rest.futi.no",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if comment is None:
raise HTTPException(404, "comment not found")

return comment
return app


@app.post("/mark-lead-as-irrelevant")
def mark_lead_as_irrelevant(false_lead: FalseLead):
# Mark the lead as irrelevant in 'leads' table
lead = db.update_lead(id=false_lead.lead_id, status="rejected")
def run():
app = create_app()

if lead is None:
raise HTTPException(404, "lead not found")

# Mark the submission as irrelevant in 'evaluated_submissions' table as a
# human answer and review
db.update_human_review_for_submission(
id=false_lead.submission_id,
human_answer=False,
correct_reason=false_lead.correct_reason,
)
uvicorn.run(app, host="0.0.0.0", port=8000, log_config="logging.yaml")

return {"status": "success"}


@app.post("/publish-comment")
def publish_comment(publish_request: PublishCommentRequest):
updated_content = comment_bot.publish_comment(
publish_request.lead_id,
publish_request.comment,
publish_request.reddit_username,
publish_request.reddit_password,
)
if updated_content is None:
raise HTTPException(404, "lead not found")

return updated_content
if __name__ == "__main__":
run()
9 changes: 7 additions & 2 deletions apps/rest/api/src/comment_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ def generate_comment(title: str, selftext: str, instructions: str = "") -> str:
# Post
Title: {title}
Content: {selftext}

# Additional instructions
{instructions}
"""
)

prompt = PromptTemplate(
template=template,
input_variables=["title", "selftext"],
input_variables=["title", "selftext", "instructions"],
partial_variables={"format_instructions": parser.get_format_instructions()},
)

Expand All @@ -50,6 +53,7 @@ def generate_comment(title: str, selftext: str, instructions: str = "") -> str:
{
"title": title,
"selftext": selftext,
"instructions": instructions,
}
)

Expand All @@ -60,7 +64,8 @@ def publish_comment(
id, text: str, username: str, password: str
) -> PublishCommentResponse | None:
lead = db.get_lead(id)
if lead is None:
print(lead)
if lead is None or lead.status == "subscriber":
logging.error(f"Lead with id {id} not found")
return None

Expand Down
49 changes: 46 additions & 3 deletions apps/rest/api/src/interfaces/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ def post_lead(lead: Lead) -> None:
"""
supabase: Client = create_client(url, key)
logger.debug(f"Posting lead: {lead}")

# Check if lead already exists (temporary until supabase-py supports onConflict)
existing_lead = (
supabase.table("leads")
.select("*")
.eq("prospect_username", lead.prospect_username)
.execute()
)

if len(existing_lead.data) > 0:
return

supabase.table("leads").insert(
json.loads(json.dumps(lead.model_dump(), default=str))
json.loads(json.dumps(lead.dict(), default=str))
).execute()


Expand All @@ -83,8 +95,28 @@ def update_lead(
Update a lead in the database.
"""
supabase: Client = create_client(url, key)
# Create a dictionary with only non-empty values
data = {k: v for k, v in {"status": status, "last_event": last_event}.items() if v}

if status is "subscriber":
last_contacted_at = datetime.now()
# Create a dictionary with only non-empty values
data = {
k: v
for k, v in {
"status": status,
"last_event": last_event,
"last_contacted_at": str(last_contacted_at),
}.items()
if v
}
else:
data = {
k: v
for k, v in {
"status": status,
"last_event": last_event,
}.items()
if v
}

logger.debug(f"Updating lead with data: {data}")
response = supabase.table("leads").update(data).eq("id", str(id)).execute()
Expand Down Expand Up @@ -125,6 +157,17 @@ def post_evaluated_submission(saved_submission: SavedSubmission) -> None:
supabase: Client = create_client(url, key)
logger.debug(f"Posting submission: {saved_submission}")

# Check if submission already exists (temporary until supabase-py supports onConflict)
existing_submission = (
supabase.table("evaluated_submissions")
.select("*")
.eq("body", saved_submission.body)
.execute()
)

if len(existing_submission.data) > 0:
return

supabase.table("evaluated_submissions").insert(
json.loads(json.dumps(saved_submission.model_dump(), default=str))
).execute()
3 changes: 2 additions & 1 deletion apps/rest/api/src/models/reddit_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from langchain.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field, validator
from pydantic import BaseModel as PydanticBaseModel

from typing import Optional

class RedditComment(BaseModel):
comment: str = Field(description="the text of the reddit comment")
Expand All @@ -12,3 +12,4 @@ class RedditComment(BaseModel):
class GenerateCommentRequest(PydanticBaseModel):
title: str
selftext: str
instructions: str = ""
Empty file.
11 changes: 7 additions & 4 deletions apps/rest/api/src/prompts/commenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
) as file:
good_examples = file.read()

with open(os.path.join(script_dir, "bad_examples.md"), "r", encoding="utf-8") as file:
with open(os.path.join(script_dir, "bad_comment_examples.md"), "r", encoding="utf-8") as file:
bad_examples = file.read()

purpose = """
Introduce myself and Futino and quickly capture interest with a relevant value proposition, setting the stage for further engagement and relationship building.
Introduce myself and Startino and quickly capture interest with a relevant value proposition, setting the stage for further engagement and relationship building.
Sometimes, if they are simply looking for advice, write a valuable comment, rather than trying to sell them. This is because I will later try to sell them after they respond.
"""

context = f"""
Expand All @@ -27,8 +28,10 @@

guidance = """
- Keep the comment concise and to the point.
- Don't mention about cost effectiveness of Futino.
- Mention how we gaurantee our dedication through our services for equity model.
- Don't mention about cost effectiveness of Startino.
- Do not mention how we gaurantee our dedication through our services for equity model.
- Do not mention the equity for services model or anything about equity.
- You may mention that we sometimes take equity if the post is a startup.
- Do not use more than 50 words.
"""

Expand Down
Loading
Loading