jina.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. from typing import Optional
  3. from langchain.schema import HumanMessage, SystemMessage
  4. from langchain_community.chat_models import JinaChat
  5. from embedchain.config import BaseLlmConfig
  6. from embedchain.helpers.json_serializable import register_deserializable
  7. from embedchain.llm.base import BaseLlm
  8. @register_deserializable
  9. class JinaLlm(BaseLlm):
  10. def __init__(self, config: Optional[BaseLlmConfig] = None):
  11. super().__init__(config=config)
  12. if not self.config.api_key and "JINACHAT_API_KEY" not in os.environ:
  13. raise ValueError("Please set the JINACHAT_API_KEY environment variable or pass it in the config.")
  14. def get_llm_model_answer(self, prompt):
  15. response = JinaLlm._get_answer(prompt, self.config)
  16. return response
  17. @staticmethod
  18. def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
  19. messages = []
  20. if config.system_prompt:
  21. messages.append(SystemMessage(content=config.system_prompt))
  22. messages.append(HumanMessage(content=prompt))
  23. kwargs = {
  24. "temperature": config.temperature,
  25. "max_tokens": config.max_tokens,
  26. "jinachat_api_key": config.api_key or os.environ["JINACHAT_API_KEY"],
  27. "model_kwargs": {},
  28. }
  29. if config.top_p:
  30. kwargs["model_kwargs"]["top_p"] = config.top_p
  31. if config.stream:
  32. from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
  33. chat = JinaChat(**kwargs, streaming=config.stream, callbacks=[StreamingStdOutCallbackHandler()])
  34. else:
  35. chat = JinaChat(**kwargs)
  36. return chat(messages).content