Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

retrieve.py 2.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  1. import logging
  2. import os
  3. from typing import Any, Dict, List, Tuple
  4. from langchain.prompts import ChatPromptTemplate
  5. from langchain.schema import AIMessage, HumanMessage
  6. from langchain_chroma import Chroma
  7. from langchain_core.documents import Document
  8. from langchain_openai import ChatOpenAI, OpenAIEmbeddings
  9. # Constants
  10. CHROMA_PATH: str = "db"
  11. OPENAI_AI_MODEL: str = "gpt-4o-mini"
  12. OPENAI_API_KEY: str | None = os.getenv("OPENAI_API_KEY")
  13. OPENAI_AI_EMBEDDING_MODEL: str = "text-embedding-3-large"
  14. # Initialize embeddings and load the Chroma database
  15. embeddings: OpenAIEmbeddings = OpenAIEmbeddings(
  16. model=OPENAI_AI_EMBEDDING_MODEL, openai_api_key=OPENAI_API_KEY
  17. )
  18. db_chroma: Chroma = Chroma(
  19. collection_name="rag_collection",
  20. persist_directory=CHROMA_PATH,
  21. embedding_function=embeddings,
  22. )
  23. # Prompt template for generating answers
  24. PROMPT_TEMPLATE: str = """
  25. Answer the question based only on the following context:
  26. {context}
  27. Answer the question based on the above context: {question}.
  28. Provide a detailed answer.
  29. Don't justify your answers.
  30. Don't give information not mentioned in the CONTEXT INFORMATION.
  31. Do not say "according to the context" or "mentioned in the context" or similar.
  32. """
  33. def call_api(
  34. prompt: str, options: Dict[str, Any], context: Dict[str, Any]
  35. ) -> Dict[str, str]:
  36. """
  37. Process a prompt using RAG and return the response.
  38. Args:
  39. prompt: The user's question or prompt
  40. options: Configuration options including topK
  41. context: Additional context for the request
  42. Returns:
  43. Dict containing the model's response
  44. Raises:
  45. Exception: If there's an error during processing
  46. """
  47. try:
  48. k: int = options.get("config", {}).get("topK", 5)
  49. docs_chroma: List[Tuple[Document, float]] = (
  50. db_chroma.similarity_search_with_score(
  51. prompt,
  52. k=k,
  53. )
  54. )
  55. context_text: str = "\n\n".join(
  56. [doc.page_content for doc, _score in docs_chroma]
  57. )
  58. # Generate prompt using the template
  59. prompt_template: ChatPromptTemplate = ChatPromptTemplate.from_template(
  60. PROMPT_TEMPLATE
  61. )
  62. final_prompt: str = prompt_template.format(
  63. context=context_text, question=prompt
  64. )
  65. # Fetch from OpenAI API
  66. chat: ChatOpenAI = ChatOpenAI(
  67. model_name=OPENAI_AI_MODEL, temperature=0, openai_api_key=OPENAI_API_KEY
  68. )
  69. message: HumanMessage = HumanMessage(content=final_prompt)
  70. response: AIMessage = chat.invoke([message])
  71. result: Dict[str, str] = {
  72. "output": response.content,
  73. }
  74. return result
  75. except Exception as e:
  76. logging.error(f"Error in call_api: {str(e)}")
  77. raise
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...