Compare commits
4 Commits
7453f5d8d7
...
1e68617a46
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e68617a46 | |||
| 484e16de4d | |||
| 1b52643367 | |||
| 45096bff6e |
@ -1,7 +1,7 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from abc import abstractmethod
|
|
||||||
from typing import Protocol, Optional, Union
|
from typing import Protocol, Optional, Union
|
||||||
from .configuration import AIConfig
|
from .configuration import AIConfig
|
||||||
|
from .tags import Tag
|
||||||
from .message import Message
|
from .message import Message
|
||||||
from .chat import Chat
|
from .chat import Chat
|
||||||
|
|
||||||
@ -36,11 +36,11 @@ class AI(Protocol):
|
|||||||
name: str
|
name: str
|
||||||
config: AIConfig
|
config: AIConfig
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def request(self,
|
def request(self,
|
||||||
question: Message,
|
question: Message,
|
||||||
context: Chat,
|
context: Chat,
|
||||||
num_answers: int = 1) -> AIResponse:
|
num_answers: int = 1,
|
||||||
|
otags: Optional[set[Tag]] = None) -> AIResponse:
|
||||||
"""
|
"""
|
||||||
Make an AI request, asking the given question with the given
|
Make an AI request, asking the given question with the given
|
||||||
context (i. e. chat history). The nr. of requested answers
|
context (i. e. chat history). The nr. of requested answers
|
||||||
@ -48,7 +48,6 @@ class AI(Protocol):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def models(self) -> list[str]:
|
def models(self) -> list[str]:
|
||||||
"""
|
"""
|
||||||
Return all models supported by this AI.
|
Return all models supported by this AI.
|
||||||
|
|||||||
20
chatmastermind/ai_factory.py
Normal file
20
chatmastermind/ai_factory.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
"""
|
||||||
|
Creates different AI instances, based on the given configuration.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
from .configuration import Config
|
||||||
|
from .ai import AI, AIError
|
||||||
|
from .ais.openai import OpenAI
|
||||||
|
|
||||||
|
|
||||||
|
def create_ai(args: argparse.Namespace, config: Config) -> AI:
|
||||||
|
"""
|
||||||
|
Creates an AI subclass instance from the given args and configuration.
|
||||||
|
"""
|
||||||
|
if args.ai == 'openai':
|
||||||
|
# FIXME: create actual 'OpenAIConfig' and set values from 'args'
|
||||||
|
# FIXME: use actual name from config
|
||||||
|
return OpenAI("openai", config.openai)
|
||||||
|
else:
|
||||||
|
raise AIError(f"AI '{args.ai}' is not supported")
|
||||||
@ -2,12 +2,12 @@
|
|||||||
Implements the OpenAI client classes and functions.
|
Implements the OpenAI client classes and functions.
|
||||||
"""
|
"""
|
||||||
import openai
|
import openai
|
||||||
from typing import Optional
|
from typing import Optional, Union
|
||||||
from ..tags import Tag
|
from ..tags import Tag
|
||||||
from ..message import Message, Answer
|
from ..message import Message, Answer
|
||||||
from ..chat import Chat
|
from ..chat import Chat
|
||||||
from ..ai import AI, AIResponse, Tokens
|
from ..ai import AI, AIResponse, Tokens
|
||||||
from ..config import OpenAIConfig
|
from ..configuration import OpenAIConfig
|
||||||
|
|
||||||
ChatType = list[dict[str, str]]
|
ChatType = list[dict[str, str]]
|
||||||
|
|
||||||
@ -17,7 +17,9 @@ class OpenAI(AI):
|
|||||||
The OpenAI AI client.
|
The OpenAI AI client.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
config: OpenAIConfig
|
def __init__(self, name: str, config: OpenAIConfig) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.config = config
|
||||||
|
|
||||||
def request(self,
|
def request(self,
|
||||||
question: Message,
|
question: Message,
|
||||||
@ -29,7 +31,8 @@ class OpenAI(AI):
|
|||||||
chat history. The nr. of requested answers corresponds to the
|
chat history. The nr. of requested answers corresponds to the
|
||||||
nr. of messages in the 'AIResponse'.
|
nr. of messages in the 'AIResponse'.
|
||||||
"""
|
"""
|
||||||
oai_chat = self.openai_chat(chat, self.config.system, question)
|
# FIXME: use real 'system' message (store in OpenAIConfig)
|
||||||
|
oai_chat = self.openai_chat(chat, "system", question)
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
model=self.config.model,
|
model=self.config.model,
|
||||||
messages=oai_chat,
|
messages=oai_chat,
|
||||||
@ -88,3 +91,6 @@ class OpenAI(AI):
|
|||||||
if question:
|
if question:
|
||||||
append('user', question.question)
|
append('user', question.question)
|
||||||
return oai_chat
|
return oai_chat
|
||||||
|
|
||||||
|
def tokens(self, data: Union[Message, Chat]) -> int:
|
||||||
|
raise NotImplementedError
|
||||||
|
|||||||
@ -12,6 +12,7 @@ from .api_client import ai, openai_api_key, print_models
|
|||||||
from .configuration import Config
|
from .configuration import Config
|
||||||
from .chat import ChatDB
|
from .chat import ChatDB
|
||||||
from .message import Message, MessageFilter, MessageError, Question
|
from .message import Message, MessageFilter, MessageError, Question
|
||||||
|
from .ai_factory import create_ai
|
||||||
from itertools import zip_longest
|
from itertools import zip_longest
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -100,11 +101,15 @@ def question_cmd(args: argparse.Namespace, config: Config) -> None:
|
|||||||
chat.add_to_cache([message])
|
chat.add_to_cache([message])
|
||||||
if args.create:
|
if args.create:
|
||||||
return
|
return
|
||||||
elif args.ask:
|
|
||||||
|
# create the correct AI instance
|
||||||
|
ai = create_ai(args, config)
|
||||||
|
if args.ask:
|
||||||
|
ai.request(message,
|
||||||
|
chat,
|
||||||
|
args.num_answers, # FIXME
|
||||||
|
args.otags) # FIXME
|
||||||
# TODO:
|
# TODO:
|
||||||
# * select the correct AIConfig
|
|
||||||
# * modify it according to the given arguments
|
|
||||||
# * create AI instance and make AI request
|
|
||||||
# * add answer to the message above (and create
|
# * add answer to the message above (and create
|
||||||
# more messages for any additional answers)
|
# more messages for any additional answers)
|
||||||
pass
|
pass
|
||||||
@ -132,7 +137,7 @@ def ask_cmd(args: argparse.Namespace, config: Config) -> None:
|
|||||||
chat, question, tags = create_question_with_hist(args, config)
|
chat, question, tags = create_question_with_hist(args, config)
|
||||||
print_chat_hist(chat, False, args.source_code_only)
|
print_chat_hist(chat, False, args.source_code_only)
|
||||||
otags = args.output_tags or []
|
otags = args.output_tags or []
|
||||||
answers, usage = ai(chat, config, args.number)
|
answers, usage = ai(chat, config, args.num_answers)
|
||||||
save_answers(question, answers, tags, otags, config)
|
save_answers(question, answers, tags, otags, config)
|
||||||
print("-" * terminal_width())
|
print("-" * terminal_width())
|
||||||
print(f"Usage: {usage}")
|
print(f"Usage: {usage}")
|
||||||
@ -212,7 +217,7 @@ def create_parser() -> argparse.ArgumentParser:
|
|||||||
question_cmd_parser.add_argument('-T', '--temperature', help='Temperature to use', type=float)
|
question_cmd_parser.add_argument('-T', '--temperature', help='Temperature to use', type=float)
|
||||||
question_cmd_parser.add_argument('-A', '--AI', help='AI to use')
|
question_cmd_parser.add_argument('-A', '--AI', help='AI to use')
|
||||||
question_cmd_parser.add_argument('-M', '--model', help='Model to use')
|
question_cmd_parser.add_argument('-M', '--model', help='Model to use')
|
||||||
question_cmd_parser.add_argument('-n', '--number', help='Number of answers to produce', type=int,
|
question_cmd_parser.add_argument('-n', '--num-answers', help='Number of answers to produce', type=int,
|
||||||
default=1)
|
default=1)
|
||||||
question_cmd_parser.add_argument('-s', '--source', nargs='+', help='Source add content of a file to the query')
|
question_cmd_parser.add_argument('-s', '--source', nargs='+', help='Source add content of a file to the query')
|
||||||
question_cmd_parser.add_argument('-S', '--source-code-only', help='Add pure source code to the chat history',
|
question_cmd_parser.add_argument('-S', '--source-code-only', help='Add pure source code to the chat history',
|
||||||
@ -228,7 +233,7 @@ def create_parser() -> argparse.ArgumentParser:
|
|||||||
ask_cmd_parser.add_argument('-m', '--max-tokens', help='Max tokens to use', type=int)
|
ask_cmd_parser.add_argument('-m', '--max-tokens', help='Max tokens to use', type=int)
|
||||||
ask_cmd_parser.add_argument('-T', '--temperature', help='Temperature to use', type=float)
|
ask_cmd_parser.add_argument('-T', '--temperature', help='Temperature to use', type=float)
|
||||||
ask_cmd_parser.add_argument('-M', '--model', help='Model to use')
|
ask_cmd_parser.add_argument('-M', '--model', help='Model to use')
|
||||||
ask_cmd_parser.add_argument('-n', '--number', help='Number of answers to produce', type=int,
|
ask_cmd_parser.add_argument('-n', '--num-answers', help='Number of answers to produce', type=int,
|
||||||
default=1)
|
default=1)
|
||||||
ask_cmd_parser.add_argument('-s', '--source', nargs='+', help='Source add content of a file to the query')
|
ask_cmd_parser.add_argument('-s', '--source', nargs='+', help='Source add content of a file to the query')
|
||||||
ask_cmd_parser.add_argument('-S', '--source-code-only', help='Add pure source code to the chat history',
|
ask_cmd_parser.add_argument('-S', '--source-code-only', help='Add pure source code to the chat history',
|
||||||
|
|||||||
@ -121,7 +121,7 @@ class TestHandleQuestion(CmmTestCase):
|
|||||||
question=[self.question],
|
question=[self.question],
|
||||||
source=None,
|
source=None,
|
||||||
source_code_only=False,
|
source_code_only=False,
|
||||||
number=3,
|
num_answers=3,
|
||||||
max_tokens=None,
|
max_tokens=None,
|
||||||
temperature=None,
|
temperature=None,
|
||||||
model=None,
|
model=None,
|
||||||
@ -158,7 +158,7 @@ class TestHandleQuestion(CmmTestCase):
|
|||||||
self.args.source_code_only)
|
self.args.source_code_only)
|
||||||
mock_ai.assert_called_with("test_chat",
|
mock_ai.assert_called_with("test_chat",
|
||||||
self.config,
|
self.config,
|
||||||
self.args.number)
|
self.args.num_answers)
|
||||||
expected_calls = []
|
expected_calls = []
|
||||||
for num, answer in enumerate(mock_ai.return_value[0], start=1):
|
for num, answer in enumerate(mock_ai.return_value[0], start=1):
|
||||||
title = f'-- ANSWER {num} '
|
title = f'-- ANSWER {num} '
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user