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