2023-10-26 18:37:41 +00:00
|
|
|
import os
|
2024-01-11 20:02:46 +00:00
|
|
|
from datetime import datetime
|
2023-10-26 18:37:41 +00:00
|
|
|
|
2023-12-28 12:34:02 +00:00
|
|
|
import factory
|
2024-01-11 20:02:46 +00:00
|
|
|
from django.utils.timezone import make_aware
|
2023-12-28 12:34:02 +00:00
|
|
|
|
2023-11-21 18:56:04 +00:00
|
|
|
from khoj.database.models import (
|
2024-12-09 02:02:29 +00:00
|
|
|
AiModelApi,
|
2023-11-02 17:43:27 +00:00
|
|
|
ChatModelOptions,
|
2023-12-28 12:34:02 +00:00
|
|
|
Conversation,
|
|
|
|
KhojApiUser,
|
|
|
|
KhojUser,
|
2024-04-17 07:52:41 +00:00
|
|
|
ProcessLock,
|
2023-11-16 01:12:54 +00:00
|
|
|
SearchModelConfig,
|
2023-11-11 06:38:28 +00:00
|
|
|
Subscription,
|
2023-12-28 12:34:02 +00:00
|
|
|
UserConversationConfig,
|
2023-10-26 18:37:41 +00:00
|
|
|
)
|
2024-11-19 00:05:15 +00:00
|
|
|
from khoj.processor.conversation.utils import message_to_log
|
2023-10-26 18:37:41 +00:00
|
|
|
|
|
|
|
|
Run online, offine chat actor, director tests for any supported provider
- Previously online chat actors, director tests only worked with openai.
This change allows running them for any supported onlnie provider
including Google, Anthropic and Openai.
- Enable online/offline chat actor, director in two ways:
1. Explicitly setting KHOJ_TEST_CHAT_PROVIDER environment variable to
google, anthropic, openai, offline
2. Implicitly by the first API key found from openai, google or anthropic.
- Default offline chat provider to use Llama 3.1 3B for faster, lower
compute test runs
2024-11-18 22:19:09 +00:00
|
|
|
def get_chat_provider(default: ChatModelOptions.ModelType | None = ChatModelOptions.ModelType.OFFLINE):
|
|
|
|
provider = os.getenv("KHOJ_TEST_CHAT_PROVIDER")
|
|
|
|
if provider and provider in ChatModelOptions.ModelType:
|
|
|
|
return ChatModelOptions.ModelType(provider)
|
|
|
|
elif os.getenv("OPENAI_API_KEY"):
|
|
|
|
return ChatModelOptions.ModelType.OPENAI
|
|
|
|
elif os.getenv("GEMINI_API_KEY"):
|
|
|
|
return ChatModelOptions.ModelType.GOOGLE
|
|
|
|
elif os.getenv("ANTHROPIC_API_KEY"):
|
|
|
|
return ChatModelOptions.ModelType.ANTHROPIC
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
def get_chat_api_key(provider: ChatModelOptions.ModelType = None):
|
|
|
|
provider = provider or get_chat_provider()
|
|
|
|
if provider == ChatModelOptions.ModelType.OPENAI:
|
|
|
|
return os.getenv("OPENAI_API_KEY")
|
|
|
|
elif provider == ChatModelOptions.ModelType.GOOGLE:
|
|
|
|
return os.getenv("GEMINI_API_KEY")
|
|
|
|
elif provider == ChatModelOptions.ModelType.ANTHROPIC:
|
|
|
|
return os.getenv("ANTHROPIC_API_KEY")
|
|
|
|
else:
|
|
|
|
return os.getenv("OPENAI_API_KEY") or os.getenv("GEMINI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
|
|
|
|
|
|
|
|
|
2024-11-19 00:05:15 +00:00
|
|
|
def generate_chat_history(message_list):
|
|
|
|
# Generate conversation logs
|
|
|
|
conversation_log = {"chat": []}
|
|
|
|
for user_message, chat_response, context in message_list:
|
|
|
|
message_to_log(
|
|
|
|
user_message,
|
|
|
|
chat_response,
|
|
|
|
{"context": context, "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}},
|
|
|
|
conversation_log=conversation_log.get("chat", []),
|
|
|
|
)
|
|
|
|
return conversation_log
|
|
|
|
|
|
|
|
|
2023-10-26 18:37:41 +00:00
|
|
|
class UserFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = KhojUser
|
|
|
|
|
|
|
|
username = factory.Faker("name")
|
|
|
|
email = factory.Faker("email")
|
|
|
|
password = factory.Faker("password")
|
|
|
|
uuid = factory.Faker("uuid4")
|
|
|
|
|
|
|
|
|
2023-10-26 19:33:03 +00:00
|
|
|
class ApiUserFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = KhojApiUser
|
|
|
|
|
|
|
|
user = None
|
|
|
|
name = factory.Faker("name")
|
|
|
|
token = factory.Faker("password")
|
|
|
|
|
|
|
|
|
2024-12-09 02:02:29 +00:00
|
|
|
class AiModelApiFactory(factory.django.DjangoModelFactory):
|
2024-06-09 01:46:55 +00:00
|
|
|
class Meta:
|
2024-12-09 02:02:29 +00:00
|
|
|
model = AiModelApi
|
2024-06-09 01:46:55 +00:00
|
|
|
|
Run online, offine chat actor, director tests for any supported provider
- Previously online chat actors, director tests only worked with openai.
This change allows running them for any supported onlnie provider
including Google, Anthropic and Openai.
- Enable online/offline chat actor, director in two ways:
1. Explicitly setting KHOJ_TEST_CHAT_PROVIDER environment variable to
google, anthropic, openai, offline
2. Implicitly by the first API key found from openai, google or anthropic.
- Default offline chat provider to use Llama 3.1 3B for faster, lower
compute test runs
2024-11-18 22:19:09 +00:00
|
|
|
api_key = get_chat_api_key()
|
2024-06-09 01:46:55 +00:00
|
|
|
|
|
|
|
|
2023-11-02 17:43:27 +00:00
|
|
|
class ChatModelOptionsFactory(factory.django.DjangoModelFactory):
|
2023-10-26 18:37:41 +00:00
|
|
|
class Meta:
|
2023-11-02 17:43:27 +00:00
|
|
|
model = ChatModelOptions
|
2023-10-26 18:37:41 +00:00
|
|
|
|
Run online, offine chat actor, director tests for any supported provider
- Previously online chat actors, director tests only worked with openai.
This change allows running them for any supported onlnie provider
including Google, Anthropic and Openai.
- Enable online/offline chat actor, director in two ways:
1. Explicitly setting KHOJ_TEST_CHAT_PROVIDER environment variable to
google, anthropic, openai, offline
2. Implicitly by the first API key found from openai, google or anthropic.
- Default offline chat provider to use Llama 3.1 3B for faster, lower
compute test runs
2024-11-18 22:19:09 +00:00
|
|
|
max_prompt_size = 20000
|
2023-10-26 18:37:41 +00:00
|
|
|
tokenizer = None
|
Run online, offine chat actor, director tests for any supported provider
- Previously online chat actors, director tests only worked with openai.
This change allows running them for any supported onlnie provider
including Google, Anthropic and Openai.
- Enable online/offline chat actor, director in two ways:
1. Explicitly setting KHOJ_TEST_CHAT_PROVIDER environment variable to
google, anthropic, openai, offline
2. Implicitly by the first API key found from openai, google or anthropic.
- Default offline chat provider to use Llama 3.1 3B for faster, lower
compute test runs
2024-11-18 22:19:09 +00:00
|
|
|
chat_model = "bartowski/Meta-Llama-3.2-3B-Instruct-GGUF"
|
|
|
|
model_type = get_chat_provider()
|
2024-12-09 02:02:29 +00:00
|
|
|
ai_model_api = factory.LazyAttribute(lambda obj: AiModelApiFactory() if get_chat_api_key() else None)
|
2023-11-02 17:43:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserConversationProcessorConfigFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = UserConversationConfig
|
|
|
|
|
|
|
|
user = factory.SubFactory(UserFactory)
|
|
|
|
setting = factory.SubFactory(ChatModelOptionsFactory)
|
2023-10-26 18:37:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConversationFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = Conversation
|
|
|
|
|
|
|
|
user = factory.SubFactory(UserFactory)
|
2023-11-11 06:38:28 +00:00
|
|
|
|
|
|
|
|
2023-11-15 00:56:26 +00:00
|
|
|
class SearchModelFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
2023-11-16 01:12:54 +00:00
|
|
|
model = SearchModelConfig
|
2023-11-15 00:56:26 +00:00
|
|
|
|
|
|
|
name = "default"
|
|
|
|
model_type = "text"
|
|
|
|
bi_encoder = "thenlper/gte-small"
|
2024-04-24 03:43:14 +00:00
|
|
|
cross_encoder = "mixedbread-ai/mxbai-rerank-xsmall-v1"
|
2023-11-15 00:56:26 +00:00
|
|
|
|
|
|
|
|
2023-11-11 06:38:28 +00:00
|
|
|
class SubscriptionFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = Subscription
|
|
|
|
|
|
|
|
user = factory.SubFactory(UserFactory)
|
2024-10-23 22:29:23 +00:00
|
|
|
type = Subscription.Type.STANDARD
|
2023-11-11 06:38:28 +00:00
|
|
|
is_recurring = False
|
2024-01-11 20:02:46 +00:00
|
|
|
renewal_date = make_aware(datetime.strptime("2100-04-01", "%Y-%m-%d"))
|
2024-04-17 07:52:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProcessLockFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = ProcessLock
|
|
|
|
|
|
|
|
name = "test_lock"
|