2023-10-26 20:37:41 +02:00
|
|
|
import os
|
2024-01-11 21:02:46 +01:00
|
|
|
from datetime import datetime
|
2023-10-26 20:37:41 +02:00
|
|
|
|
2023-12-28 13:34:02 +01:00
|
|
|
import factory
|
2024-01-11 21:02:46 +01:00
|
|
|
from django.utils.timezone import make_aware
|
2023-12-28 13:34:02 +01:00
|
|
|
|
2023-11-21 19:56:04 +01:00
|
|
|
from khoj.database.models import (
|
2023-11-02 18:43:27 +01:00
|
|
|
ChatModelOptions,
|
2023-12-28 13:34:02 +01:00
|
|
|
Conversation,
|
|
|
|
KhojApiUser,
|
|
|
|
KhojUser,
|
2023-10-26 20:37:41 +02:00
|
|
|
OpenAIProcessorConversationConfig,
|
2024-04-17 09:52:41 +02:00
|
|
|
ProcessLock,
|
2023-11-16 02:12:54 +01:00
|
|
|
SearchModelConfig,
|
2023-11-11 07:38:28 +01:00
|
|
|
Subscription,
|
2023-12-28 13:34:02 +01:00
|
|
|
UserConversationConfig,
|
2023-10-26 20:37:41 +02:00
|
|
|
)
|
2024-11-19 01:05:15 +01:00
|
|
|
from khoj.processor.conversation.utils import message_to_log
|
2023-10-26 20:37:41 +02: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 23:19:09 +01: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 01:05:15 +01: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 20:37:41 +02: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 21:33:03 +02:00
|
|
|
class ApiUserFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = KhojApiUser
|
|
|
|
|
|
|
|
user = None
|
|
|
|
name = factory.Faker("name")
|
|
|
|
token = factory.Faker("password")
|
|
|
|
|
|
|
|
|
2024-06-09 03:46:55 +02:00
|
|
|
class OpenAIProcessorConversationConfigFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = OpenAIProcessorConversationConfig
|
|
|
|
|
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 23:19:09 +01:00
|
|
|
api_key = get_chat_api_key()
|
2024-06-09 03:46:55 +02:00
|
|
|
|
|
|
|
|
2023-11-02 18:43:27 +01:00
|
|
|
class ChatModelOptionsFactory(factory.django.DjangoModelFactory):
|
2023-10-26 20:37:41 +02:00
|
|
|
class Meta:
|
2023-11-02 18:43:27 +01:00
|
|
|
model = ChatModelOptions
|
2023-10-26 20:37:41 +02: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 23:19:09 +01:00
|
|
|
max_prompt_size = 20000
|
2023-10-26 20:37:41 +02: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 23:19:09 +01:00
|
|
|
chat_model = "bartowski/Meta-Llama-3.2-3B-Instruct-GGUF"
|
|
|
|
model_type = get_chat_provider()
|
2024-06-18 16:01:07 +02:00
|
|
|
openai_config = factory.LazyAttribute(
|
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 23:19:09 +01:00
|
|
|
lambda obj: OpenAIProcessorConversationConfigFactory() if get_chat_api_key() else None
|
2024-06-18 16:01:07 +02:00
|
|
|
)
|
2023-11-02 18:43:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserConversationProcessorConfigFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = UserConversationConfig
|
|
|
|
|
|
|
|
user = factory.SubFactory(UserFactory)
|
|
|
|
setting = factory.SubFactory(ChatModelOptionsFactory)
|
2023-10-26 20:37:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ConversationFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = Conversation
|
|
|
|
|
|
|
|
user = factory.SubFactory(UserFactory)
|
2023-11-11 07:38:28 +01:00
|
|
|
|
|
|
|
|
2023-11-15 01:56:26 +01:00
|
|
|
class SearchModelFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
2023-11-16 02:12:54 +01:00
|
|
|
model = SearchModelConfig
|
2023-11-15 01:56:26 +01:00
|
|
|
|
|
|
|
name = "default"
|
|
|
|
model_type = "text"
|
|
|
|
bi_encoder = "thenlper/gte-small"
|
2024-04-24 05:43:14 +02:00
|
|
|
cross_encoder = "mixedbread-ai/mxbai-rerank-xsmall-v1"
|
2023-11-15 01:56:26 +01:00
|
|
|
|
|
|
|
|
2023-11-11 07:38:28 +01:00
|
|
|
class SubscriptionFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = Subscription
|
|
|
|
|
|
|
|
user = factory.SubFactory(UserFactory)
|
2024-10-24 00:29:23 +02:00
|
|
|
type = Subscription.Type.STANDARD
|
2023-11-11 07:38:28 +01:00
|
|
|
is_recurring = False
|
2024-01-11 21:02:46 +01:00
|
|
|
renewal_date = make_aware(datetime.strptime("2100-04-01", "%Y-%m-%d"))
|
2024-04-17 09:52:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ProcessLockFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = ProcessLock
|
|
|
|
|
|
|
|
name = "test_lock"
|