Skip to content

Commit

Permalink
✨ add system role in GPT
Browse files Browse the repository at this point in the history
  • Loading branch information
agn-7 committed Dec 25, 2023
1 parent dffa64d commit fb326d1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
3 changes: 1 addition & 2 deletions ifsguid/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException, status
from g4f import ModelUtils

from . import crud, schemas, modules
from .database import async_session, AsyncSession
Expand Down Expand Up @@ -120,7 +119,7 @@ async def create_message(
if message.role == "human":
ai_content = await modules.generate_ai_response(
content=message.content,
model=ModelUtils.convert[interaction.settings.model],
interaction=interaction,
)
ai_message = schemas.MessageCreate(role="ai", content=ai_content)

Expand Down
13 changes: 8 additions & 5 deletions ifsguid/modules.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import g4f
import traceback

from .schemas import Interaction

g4f.debug.logging = True
g4f.check_version = False


async def generate_ai_response(
content: str, model: g4f.Model = g4f.models.default
) -> str:
async def generate_ai_response(content: str, interaction: Interaction) -> str:
try:
response = await g4f.ChatCompletion.create_async(
model=model,
messages=[{"role": "human", "content": content}],
model=g4f.ModelUtils.convert[interaction.settings.model],
messages=[
{"role": "system", "content": interaction.settings.prompt},
{"role": "user", "content": content},
],
)
return response
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion ifsguid/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ChatModel(BaseModel):


class Prompt(BaseModel):
role: Literal["System"] = "System"
role: Literal["system"] = "system"
prompt: str


Expand Down
2 changes: 1 addition & 1 deletion tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ async def test_create_interaction(db):
assert response.json()["settings"] == {
"prompt": "something",
"model": "gpt-3.5-turbo",
"role": "System",
"role": "system",
}

0 comments on commit fb326d1

Please sign in to comment.