Skip to content

Commit

Permalink
WIP: Small refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Chmutov <ulman.andre@gmail.com>
  • Loading branch information
AndrewChmutov committed Nov 25, 2024
1 parent 7ee9cfb commit c0d67c0
Show file tree
Hide file tree
Showing 48 changed files with 1,212 additions and 911 deletions.
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
exclude: (.*/thirdparty/.*)

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.5
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/jsh9/pydoclint
rev: 0.3.4
hooks:
- id: pydoclint
- repo: https://github.com/crate-ci/typos
rev: v1.16.23
hooks:
- id: typos
4 changes: 2 additions & 2 deletions poetry.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[virtualenvs]
create = true
[virtualenvs]
create = true
in-project = true
34 changes: 33 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ python = "^3.11"
openai = "^1.52.2"
pydantic = "^2.9.2"
langchain = "^0.3.4"
langchain-community = "^0.3.3"
langchain-community = "^0.3.3"
langchain-openai = "^0.2.3"
langchain-google-genai = "^2.0.1"
streamlit = "^1.39.0"
Expand All @@ -32,3 +32,35 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
main = "scripts.main:main"
tournament = "scripts.tournament:main"

[tool.ruff]
preview = true

[tool.ruff.lint]
select = [
"ANN001", # Missing type annotation for function argument
"DOC", # Pydoclint
"D", # Pydocstyle
"E", # Pycodestyle
"F", # Pyflakes
"I", # Isort
"Q", # Quotes
]

ignore = [
"D1", # Missing docstrings
"D205",
]

[tool.ruff.lint.flake8-quotes]
inline-quotes = "double"

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.format]
quote-style = "double"

[tool.pydoclint]
style = "google"
arg-type-hints-in-signature = true
22 changes: 0 additions & 22 deletions raw_development_history.md

This file was deleted.

11 changes: 8 additions & 3 deletions scripts/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import os
import subprocess


def main():
# Define the project root directory (this assumes the script is located in the scripts/ folder)
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
os.chdir(project_root)
main_path = os.path.join(project_root, "src", "llm_postor", "main.py")

if not os.path.exists(main_path):
raise FileNotFoundError(f"File does not exist: {main_path}")

# Run the Streamlit app with the correct path
subprocess.run(["poetry", "run", "streamlit", "run", "--server.runOnSave", "True", main_path], check=True)
subprocess.run(
["poetry", "run", "streamlit", "run", "--server.runOnSave", "True", main_path],
check=True,
)


if __name__ == "__main__":
main()
main()
6 changes: 4 additions & 2 deletions scripts/tournament.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import subprocess


def main():
# Define the project root directory (this assumes the script is located in the scripts/ folder)
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
os.chdir(project_root)
tournament_path = os.path.join(project_root, "src", "llm_postor", "tournament.py")

Expand All @@ -13,5 +14,6 @@ def main():
# Run the Streamlit app with the correct path
subprocess.run(["poetry", "run", "python3", tournament_path], check=True)


if __name__ == "__main__":
main()
main()
File renamed without changes.
38 changes: 38 additions & 0 deletions src/among_them/annotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from langchain.schema import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI

from among_them.config import OPENROUTER_API_KEY
from among_them.game.llm_prompts import ANNOTATION_SYSTEM_PROMPT


def annotate_dialogue(dialogue: str, llm_model_name: str = "openai/gpt-4o-mini") -> str:
"""Annotates a dialogue with persuasion techniques using OpenAI API.
Args:
dialogue: The dialogue to annotate.
llm_model_name: The OpenAI model to use for annotation.
Returns:
The annotated dialogue in the specified format.
"""
try:
llm = ChatOpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=OPENROUTER_API_KEY,
model=llm_model_name,
temperature=0.1,
)
prompt = ANNOTATION_SYSTEM_PROMPT.format()
response = llm.invoke([
SystemMessage(content=prompt),
HumanMessage(content=dialogue),
])

# Extract the annotated text from the response
annotated_text = response.content.strip()
except Exception as e:
print(f"Error: {e} while annotating the dialogue: {dialogue}\n", e)
print("Returning empty string...")
annotated_text = ""

return annotated_text
8 changes: 5 additions & 3 deletions src/llm_postor/config.py → src/among_them/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

from dotenv import load_dotenv

# Load environment variables from .env only if not already set
Expand All @@ -13,8 +14,9 @@
"API key is missing. Please set OPENROUTER_API_KEY "
"in your environment or in a .env file in the project root."
)

# if src/llm_postor/game/dummy.py does not exist, create it. This is for abusing streamlit refresh when game_state.json changes

# if src/llm_postor/game/dummy.py does not exist, create it.
# This is for abusing streamlit refresh when game_state.json changes
if not os.path.exists("src/llm_postor/game/dummy.py"):
with open("src/llm_postor/game/dummy.py", "w") as f:
f.write("timestamp = '2024-11-15 00:20:17.946790'")
f.write("timestamp = '2024-11-15 00:20:17.946790'")
File renamed without changes.
11 changes: 11 additions & 0 deletions src/among_them/game/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .adventure import AdventureAgent
from .base import Agent
from .discussion import DiscussionAgent
from .voting import VotingAgent

__all__ = [
"AdventureAgent",
"Agent",
"DiscussionAgent",
"VotingAgent",
]
Loading

0 comments on commit c0d67c0

Please sign in to comment.