App.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. messages.append({"role": "user", "content": prompt})
  22. response = openai.ChatCompletion.create(
  23. model=config.model or "gpt-3.5-turbo-0613",
  24. messages=messages,
  25. temperature=config.temperature,
  26. max_tokens=config.max_tokens,
  27. top_p=config.top_p,
  28. stream=config.stream,
  29. )
  30. if config.stream:
  31. return self._stream_llm_model_response(response)
  32. else:
  33. return response["choices"][0]["message"]["content"]
  34. def _stream_llm_model_response(self, response):
  35. """
  36. This is a generator for streaming response from the OpenAI completions API
  37. """
  38. for line in response:
  39. chunk = line["choices"][0].get("delta", {}).get("content", "")
  40. yield chunk