-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_ops.py
66 lines (55 loc) · 1.99 KB
/
openai_ops.py
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
"""
This module provides a class for interacting with the Azure OpenAI API.
It includes methods for generating embeddings and text responses.
"""
from typing import List
from openai import AzureOpenAI
class OpenAIOperations:
def __init__(self, api_key: str):
"""
Initialize the OpenAIOperations class.
Args:
api_key (str): The API key for Azure OpenAI.
"""
self.client = AzureOpenAI(
azure_endpoint="https://confmind-oai3.openai.azure.com/",
api_key=api_key,
api_version="2024-02-15-preview",
)
def get_embedding(self, text: str) -> List[float]:
"""
Generate an embedding for the given text.
Args:
text (str): The input text to embed.
Returns:
List[float]: The embedding vector.
"""
response = self.client.embeddings.create(model="RAGProtoEmbed", input=text)
return response.data[0].embedding
def generate_response(self, context: str, question: str) -> str:
"""
Generate a response to a question based on the given context.
Args:
context (str): The context information.
question (str): The question to answer.
Returns:
str: The generated response.
"""
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
response = self.client.chat.completions.create(
model="RAGProto",
messages=[
{
"role": "system",
"content": "You are a helpful assistant that answers questions based on the given context.",
},
{"role": "user", "content": prompt},
],
temperature=0.7,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None,
)
return response.choices[0].message.content