App.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import openai
  2. from embedchain.config import AppConfig, ChatConfig
  3. from embedchain.embedchain import EmbedChain
  4. class App(EmbedChain):
  5. """
  6. The EmbedChain app.
  7. Has two functions: add and query.
  8. adds(data_type, url): adds the data from the given URL to the vector db.
  9. query(query): finds answer to the given query using vector database and LLM.
  10. dry_run(query): test your prompt without consuming tokens.
  11. """
  12. def __init__(self, config: AppConfig = None):
  13. """
  14. :param config: AppConfig instance to load as configuration. Optional.
  15. """
  16. if config is None:
  17. config = AppConfig()
  18. super().__init__(config)
  19. def get_llm_model_answer(self, prompt, config: ChatConfig):
  20. messages = []
  21. if config.system_prompt:
  22. messages.append({"role": "system", "content": config.system_prompt})
  23. messages.append({"role": "user", "content": prompt})
  24. response = openai.ChatCompletion.create(
  25. model=config.model or "gpt-3.5-turbo-0613",
  26. messages=messages,
  27. temperature=config.temperature,
  28. max_tokens=config.max_tokens,
  29. top_p=config.top_p,
  30. stream=config.stream,
  31. )
  32. if config.stream:
  33. return self._stream_llm_model_response(response)
  34. else:
  35. return response["choices"][0]["message"]["content"]
  36. def _stream_llm_model_response(self, response):
  37. """
  38. This is a generator for streaming response from the OpenAI completions API
  39. """
  40. for line in response:
  41. chunk = line["choices"][0].get("delta", {}).get("content", "")
  42. yield chunk