Upload 2 files
Browse files- src/api/openai_api.py +140 -0
- src/api/openai_config.py +31 -0
src/api/openai_api.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
OpenAI API integration for Norwegian RAG chatbot.
|
| 3 |
+
Provides functions to interact with OpenAI API for both GPT-4o and embedding models.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
import time
|
| 9 |
+
import requests
|
| 10 |
+
from typing import Dict, List, Optional, Union, Any
|
| 11 |
+
import openai
|
| 12 |
+
|
| 13 |
+
class OpenAIAPI:
|
| 14 |
+
"""
|
| 15 |
+
Client for interacting with OpenAI API.
|
| 16 |
+
Supports both text generation (GPT-4o) and embedding generation.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def __init__(
|
| 20 |
+
self,
|
| 21 |
+
api_key: Optional[str] = None,
|
| 22 |
+
model: str = "gpt-4o",
|
| 23 |
+
embedding_model: str = "text-embedding-3-small"
|
| 24 |
+
):
|
| 25 |
+
"""
|
| 26 |
+
Initialize the OpenAI API client.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
api_key: OpenAI API key (optional, can use OPENAI_API_KEY env var)
|
| 30 |
+
model: GPT model to use (default: gpt-4o)
|
| 31 |
+
embedding_model: Embedding model to use (default: text-embedding-3-small)
|
| 32 |
+
"""
|
| 33 |
+
self.api_key = api_key or os.environ.get("OPENAI_API_KEY", "")
|
| 34 |
+
if not self.api_key:
|
| 35 |
+
raise ValueError("OpenAI API key is required. Set it as OPENAI_API_KEY environment variable or pass it to the constructor.")
|
| 36 |
+
|
| 37 |
+
self.client = openai.OpenAI(api_key=self.api_key)
|
| 38 |
+
self.model = model
|
| 39 |
+
self.embedding_model = embedding_model
|
| 40 |
+
|
| 41 |
+
def generate_text(
|
| 42 |
+
self,
|
| 43 |
+
prompt: str,
|
| 44 |
+
max_tokens: int = 512,
|
| 45 |
+
temperature: float = 0.7,
|
| 46 |
+
top_p: float = 0.9,
|
| 47 |
+
stream: bool = False
|
| 48 |
+
) -> Union[str, Any]:
|
| 49 |
+
"""
|
| 50 |
+
Generate text using the GPT-4o model.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
prompt: Input text prompt
|
| 54 |
+
max_tokens: Maximum number of tokens to generate
|
| 55 |
+
temperature: Sampling temperature
|
| 56 |
+
top_p: Top-p sampling parameter
|
| 57 |
+
stream: Whether to stream the response
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
Generated text response or stream
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
+
messages = [{"role": "user", "content": prompt}]
|
| 64 |
+
|
| 65 |
+
response = self.client.chat.completions.create(
|
| 66 |
+
model=self.model,
|
| 67 |
+
messages=messages,
|
| 68 |
+
max_tokens=max_tokens,
|
| 69 |
+
temperature=temperature,
|
| 70 |
+
top_p=top_p,
|
| 71 |
+
stream=stream
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if stream:
|
| 75 |
+
return response
|
| 76 |
+
else:
|
| 77 |
+
return response.choices[0].message.content
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"Error generating text: {str(e)}")
|
| 81 |
+
return f"Error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
def generate_embeddings(
|
| 84 |
+
self,
|
| 85 |
+
texts: Union[str, List[str]]
|
| 86 |
+
) -> List[List[float]]:
|
| 87 |
+
"""
|
| 88 |
+
Generate embeddings for text using the embedding model.
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
texts: Single text or list of texts to embed
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
List of embedding vectors
|
| 95 |
+
"""
|
| 96 |
+
# Ensure texts is a list
|
| 97 |
+
if isinstance(texts, str):
|
| 98 |
+
texts = [texts]
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
response = self.client.embeddings.create(
|
| 102 |
+
model=self.embedding_model,
|
| 103 |
+
input=texts
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Extract embeddings from response
|
| 107 |
+
embeddings = [item.embedding for item in response.data]
|
| 108 |
+
return embeddings
|
| 109 |
+
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"Error generating embeddings: {str(e)}")
|
| 112 |
+
# Return empty embeddings as fallback
|
| 113 |
+
return [[0.0] * 1536] * len(texts)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Example RAG prompt template for Norwegian
|
| 117 |
+
def create_rag_prompt(query: str, context: List[str]) -> str:
|
| 118 |
+
"""
|
| 119 |
+
Create a RAG prompt with retrieved context for GPT-4o.
|
| 120 |
+
|
| 121 |
+
Args:
|
| 122 |
+
query: User query
|
| 123 |
+
context: List of retrieved document chunks
|
| 124 |
+
|
| 125 |
+
Returns:
|
| 126 |
+
Formatted prompt with context
|
| 127 |
+
"""
|
| 128 |
+
context_text = "\n\n".join([f"Dokument {i+1}:\n{chunk}" for i, chunk in enumerate(context)])
|
| 129 |
+
|
| 130 |
+
prompt = f"""Du er en hjelpsom assistent som svarer på norsk. Bruk følgende kontekst for å svare på spørsmålet.
|
| 131 |
+
|
| 132 |
+
KONTEKST:
|
| 133 |
+
{context_text}
|
| 134 |
+
|
| 135 |
+
SPØRSMÅL:
|
| 136 |
+
{query}
|
| 137 |
+
|
| 138 |
+
SVAR:
|
| 139 |
+
"""
|
| 140 |
+
return prompt
|
src/api/openai_config.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Updated configuration for Norwegian RAG chatbot with GPT-4o integration.
|
| 3 |
+
Contains model IDs, API endpoints, and other configuration parameters.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# OpenAI GPT-4o configuration
|
| 7 |
+
OPENAI_CONFIG = {
|
| 8 |
+
"model": "gpt-4o",
|
| 9 |
+
"embedding_model": "text-embedding-3-small",
|
| 10 |
+
"max_tokens": 512,
|
| 11 |
+
"temperature": 0.7,
|
| 12 |
+
"top_p": 0.9
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Document processing parameters
|
| 16 |
+
CHUNK_SIZE = 512
|
| 17 |
+
CHUNK_OVERLAP = 100
|
| 18 |
+
|
| 19 |
+
# RAG parameters
|
| 20 |
+
MAX_CHUNKS_TO_RETRIEVE = 5
|
| 21 |
+
SIMILARITY_THRESHOLD = 0.75
|
| 22 |
+
|
| 23 |
+
# Requirements for OpenAI integration
|
| 24 |
+
REQUIRED_PACKAGES = [
|
| 25 |
+
"openai>=1.0.0",
|
| 26 |
+
"numpy>=1.24.0",
|
| 27 |
+
"gradio>=4.0.0",
|
| 28 |
+
"PyPDF2>=3.0.0",
|
| 29 |
+
"beautifulsoup4>=4.12.0",
|
| 30 |
+
"requests>=2.31.0"
|
| 31 |
+
]
|