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

Solution with gemini #2

Open
wants to merge 10 commits into
base: solution
Choose a base branch
from
35 changes: 13 additions & 22 deletions app/routes/character_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from ..models.character import Character
from ..models.greeting import Greeting
from sqlalchemy import func, union, except_
from openai import OpenAI

import google.generativeai as genai
import os

genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))

bp = Blueprint("characters", __name__, url_prefix="/characters")
client = OpenAI()

@bp.post("")
def create_character():
Expand All @@ -30,15 +33,7 @@ def get_characters():
response = []

for character in characters:
response.append(
{
"id" : character.id,
"name" : character.name,
"personality" : character.personality,
"occupation" : character.occupation,
"age" : character.age
}
)
response.append(character.to_dict())

return jsonify(response)

Expand All @@ -58,21 +53,20 @@ def get_greetings(char_id):

return jsonify(response)


@bp.post("/<char_id>/generate")
def add_greetings(char_id):
character_obj = validate_model(Character, char_id)
greetings = generate_greetings(character_obj)
print(greetings)

if character_obj.greetings:
return make_response(jsonify(f"Greetings already generated for {character_obj.name} "), 201)

new_greetings = []

for greeting in greetings:
text = greeting[greeting.find(" ")+1:]
new_greeting = Greeting(
greeting_text = text.strip("\""),
greeting_text = greeting.strip("\""), #Removes quotes from each string
character = character_obj
)
new_greetings.append(new_greeting)
Expand All @@ -83,15 +77,12 @@ def add_greetings(char_id):
return make_response(jsonify(f"Greetings successfully added to {character_obj.name}"), 201)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove jsonify through the project


def generate_greetings(character):
model = genai.GenerativeModel("gemini-1.5-flash")
input_message = f"I am writing a rantasy RPG video game. I have an npc named {character.name} who is {character.age} years old. They are a {character.occupation} who has a {character.personality} personality. Please generate a python style list of 10 stock phrases they might use when the main character talks to them. Please Return just the list without a variable name and square brackets."
Copy link

@kelsey-steven-ada kelsey-steven-ada Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
input_message = f"I am writing a rantasy RPG video game. I have an npc named {character.name} who is {character.age} years old. They are a {character.occupation} who has a {character.personality} personality. Please generate a python style list of 10 stock phrases they might use when the main character talks to them. Please Return just the list without a variable name and square brackets."
input_message = f"I am writing a fantasy RPG video game. I have an npc named {character.name} who is {character.age} years old. They are a {character.occupation} who has a {character.personality} personality. Please generate a Python style list of 10 stock phrases they might use when the main character talks to them. Please return just the list without a variable name and square brackets."

response = model.generate_content(input_message)
response_split = response.text.split("\n") #Splits response into a list of stock phrases, ends up with an empty string at index -1
return response_split[:-1] #Returns the stock phrases list, just without the empty string at the end

input_message = f"I am writing a video game in the style of The Witcher. I have an npc named {character.name} who is {character.age} years old. They are a {character.occupation} who has a {character.personality} personality. Please generate a python style list of 10 stock phrases they might use when the main character talks to them"
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": input_message}
]
)
return(completion.choices[0].message.content.split("\n"))

def validate_model(cls,id):
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Add character model
"""empty message

Revision ID: d7a96ea32a49
Revision ID: 0344241db67d
Revises:
Create Date: 2024-06-11 11:15:08.811220
Create Date: 2024-11-05 16:05:21.795648

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd7a96ea32a49'
revision = '0344241db67d'
down_revision = None
branch_labels = None
depends_on = None
Expand All @@ -26,10 +26,18 @@ def upgrade():
sa.Column('age', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('greeting',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('greeting_text', sa.String(), nullable=False),
sa.Column('character_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['character_id'], ['character.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('greeting')
op.drop_table('character')
# ### end Alembic commands ###
34 changes: 0 additions & 34 deletions migrations/versions/657ab8362756_.py

This file was deleted.

34 changes: 0 additions & 34 deletions migrations/versions/8ed7775249e9_.py

This file was deleted.

34 changes: 0 additions & 34 deletions migrations/versions/e06848138ff6_.py

This file was deleted.

23 changes: 23 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,54 @@ alembic==1.13.1
annotated-types==0.6.0
anyio==4.3.0
blinker==1.7.0
cachetools==5.5.0
certifi==2024.2.2
charset-normalizer==3.4.0
click==8.1.7
distro==1.9.0
Flask==3.0.2
Flask-Migrate==4.0.5
Flask-SQLAlchemy==3.1.1
google-ai-generativelanguage==0.6.10
google-api-core==2.22.0
google-api-python-client==2.151.0
google-auth==2.35.0
google-auth-httplib2==0.2.0
google-generativeai==0.8.3
googleapis-common-protos==1.65.0
greenlet==3.0.3
grpcio==1.67.1
grpcio-status==1.67.1
h11==0.14.0
httpcore==1.0.5
httplib2==0.22.0
httpx==0.27.0
idna==3.7
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.3
Mako==1.3.2
MarkupSafe==2.1.5
openai==1.35.3
packaging==23.2
pluggy==1.4.0
proto-plus==1.25.0
protobuf==5.28.3
psycopg2-binary==2.9.9
pyasn1==0.6.1
pyasn1_modules==0.4.1
pydantic==2.7.1
pydantic_core==2.18.2
pyparsing==3.2.0
pytest==8.0.0
python-dotenv==1.0.1
requests==2.32.3
rsa==4.9
sniffio==1.3.1
SQLAlchemy==2.0.25
tqdm==4.66.4
typing_extensions==4.9.0
uritemplate==4.1.1
urllib3==2.2.3
Werkzeug==3.0.1
wonderwords==2.2.0
7 changes: 7 additions & 0 deletions reset_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from app import create_app, db


my_app = create_app()
with my_app.app_context():
db.session.clear()
print("Session cleared.")