Добавлена передача имени приложения и идентификатора чата в запросах к OpenRouter.
This commit is contained in:
parent
8c95f8a221
commit
4f35663784
3 changed files with 16 additions and 11 deletions
19
ai_agent.py
19
ai_agent.py
|
|
@ -21,6 +21,10 @@ PRIVATE_CHAT_SYSTEM_PROMPT = """
|
||||||
"""
|
"""
|
||||||
PRIVATE_CHAT_MAX_MESSAGES = 40
|
PRIVATE_CHAT_MAX_MESSAGES = 40
|
||||||
|
|
||||||
|
OPENROUTER_HEADERS = {
|
||||||
|
'HTTP-Referer': 'https://ultracoder.org',
|
||||||
|
'X-Title': 'TG/VK Chat Bot'
|
||||||
|
}
|
||||||
|
|
||||||
@dataclass()
|
@dataclass()
|
||||||
class Message:
|
class Message:
|
||||||
|
|
@ -30,13 +34,14 @@ class Message:
|
||||||
|
|
||||||
|
|
||||||
class AiAgent:
|
class AiAgent:
|
||||||
def __init__(self, api_token: str, model: str, db: BasicDatabase):
|
def __init__(self, api_token: str, model: str, db: BasicDatabase, platform: str):
|
||||||
retry_config = RetryConfig(strategy="backoff",
|
retry_config = RetryConfig(strategy="backoff",
|
||||||
backoff=BackoffStrategy(
|
backoff=BackoffStrategy(
|
||||||
initial_interval=2000, max_interval=8000, exponent=2, max_elapsed_time=14000),
|
initial_interval=2000, max_interval=8000, exponent=2, max_elapsed_time=14000),
|
||||||
retry_connection_errors=True)
|
retry_connection_errors=True)
|
||||||
self.db = db
|
self.db = db
|
||||||
self.model = model
|
self.model = model
|
||||||
|
self.platform = platform
|
||||||
self.client = OpenRouter(api_key=api_token, retry_config=retry_config)
|
self.client = OpenRouter(api_key=api_token, retry_config=retry_config)
|
||||||
|
|
||||||
async def get_group_chat_reply(self, bot_id: int, chat_id: int,
|
async def get_group_chat_reply(self, bot_id: int, chat_id: int,
|
||||||
|
|
@ -55,7 +60,9 @@ class AiAgent:
|
||||||
response = await self.client.chat.send_async(
|
response = await self.client.chat.send_async(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
messages=context,
|
messages=context,
|
||||||
max_tokens=500
|
max_tokens=500,
|
||||||
|
user=f'{self.platform}_{bot_id}_{chat_id}',
|
||||||
|
http_headers=OPENROUTER_HEADERS
|
||||||
)
|
)
|
||||||
|
|
||||||
# Extract AI response
|
# Extract AI response
|
||||||
|
|
@ -85,7 +92,9 @@ class AiAgent:
|
||||||
response = await self.client.chat.send_async(
|
response = await self.client.chat.send_async(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
messages=context,
|
messages=context,
|
||||||
max_tokens=500
|
max_tokens=500,
|
||||||
|
user=f'{self.platform}_{bot_id}_{chat_id}',
|
||||||
|
http_headers=OPENROUTER_HEADERS
|
||||||
)
|
)
|
||||||
|
|
||||||
# Extract AI response
|
# Extract AI response
|
||||||
|
|
@ -133,6 +142,6 @@ class AiAgent:
|
||||||
agent: AiAgent
|
agent: AiAgent
|
||||||
|
|
||||||
|
|
||||||
def create_ai_agent(api_token: str, model: str, db: BasicDatabase):
|
def create_ai_agent(api_token: str, model: str, db: BasicDatabase, platform: str):
|
||||||
global agent
|
global agent
|
||||||
agent = AiAgent(api_token, model, db)
|
agent = AiAgent(api_token, model, db, platform)
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,7 @@ async def main() -> None:
|
||||||
|
|
||||||
database.create_database(config['db_connection_string'])
|
database.create_database(config['db_connection_string'])
|
||||||
|
|
||||||
create_ai_agent(config['openrouter_token'],
|
create_ai_agent(config['openrouter_token'], config['openrouter_model'], database.DB, 'tg')
|
||||||
config['openrouter_model'],
|
|
||||||
database.DB)
|
|
||||||
|
|
||||||
bots: list[Bot] = []
|
bots: list[Bot] = []
|
||||||
for item in database.DB.get_bots():
|
for item in database.DB.get_bots():
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,7 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
database.create_database(config['db_connection_string'])
|
database.create_database(config['db_connection_string'])
|
||||||
|
|
||||||
create_ai_agent(config['openrouter_token'],
|
create_ai_agent(config['openrouter_token'], config['openrouter_model'], database.DB, 'vk')
|
||||||
config['openrouter_model'],
|
|
||||||
database.DB)
|
|
||||||
|
|
||||||
bot = Bot(labeler=handlers.labeler)
|
bot = Bot(labeler=handlers.labeler)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue