Spaces:
Running
Running
| import os | |
| from openai import OpenAI | |
| from dotenv import load_dotenv | |
| load_dotenv() # | |
| #-------------------------------------------------------- | |
| # Initialize OpenAI client | |
| #-------------------------------------------------------- | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| def embed_question_openai(texts, model="text-embedding-3-small"): | |
| response = client.embeddings.create( | |
| input=texts, | |
| model=model | |
| ) | |
| return response.data[0].embedding | |
| def openai_domain_specific_answer_generation(system_prompt, question, model="gpt4o-mini", temperature=0.3, top_p=0.1): | |
| prompt = f""" | |
| Question: | |
| {question} | |
| Answer (provide a precise, domain-specific response): | |
| """ | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": system_prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| temperature=temperature, # Set low for deterministic and precise responses. | |
| top_p=top_p, # Focus on high-probability outputs to ensure accuracy. | |
| frequency_penalty=0.1, # Reduce repetition of technical terms. | |
| presence_penalty=0.0 # Prevent introduction of unrelated ideas. | |
| ) | |
| return response.choices[0].message.content | |
| def openai_context_integration(system_prompt, query, expert_answer, retrieved_context, model="gpt4o-mini", temperature=0.3, top_p=0.3): | |
| prompt = f""" | |
| Question: | |
| {query} | |
| Direct Answer: | |
| {expert_answer} | |
| Retrieved Context: | |
| {retrieved_context} | |
| Final Answer: | |
| """ | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": system_prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| temperature=temperature, # Maintain some flexibility for smooth blending. | |
| top_p=top_p, # Prioritize high-probability outputs to stay focused on the inputs. | |
| frequency_penalty=0.1, # Allow necessary repetition for clarity. | |
| presence_penalty=0.0 # Neutral to avoid introducing unrelated ideas. | |
| ) | |
| return response.choices[0].message.content | |