atolat30 commited on
Commit
63073b9
·
1 Parent(s): 51ee036

Upgrade to GPT-4 and adjust token limits

Browse files
Files changed (2) hide show
  1. aimakerspace/openai_utils/chatmodel.py +34 -20
  2. chainlit.md +13 -2
aimakerspace/openai_utils/chatmodel.py CHANGED
@@ -6,25 +6,33 @@ load_dotenv()
6
 
7
 
8
  class ChatOpenAI:
9
- def __init__(self, model_name: str = "gpt-4o-mini"):
10
  self.model_name = model_name
11
  self.openai_api_key = os.getenv("OPENAI_API_KEY")
12
  if self.openai_api_key is None:
13
  raise ValueError("OPENAI_API_KEY is not set")
 
 
14
 
15
  def run(self, messages, text_only: bool = True, **kwargs):
16
  if not isinstance(messages, list):
17
  raise ValueError("messages must be a list")
18
 
19
  client = OpenAI()
20
- response = client.chat.completions.create(
21
- model=self.model_name, messages=messages, **kwargs
22
- )
23
-
24
- if text_only:
25
- return response.choices[0].message.content
26
-
27
- return response
 
 
 
 
 
 
28
 
29
  async def astream(self, messages, **kwargs):
30
  if not isinstance(messages, list):
@@ -32,14 +40,20 @@ class ChatOpenAI:
32
 
33
  client = AsyncOpenAI()
34
 
35
- stream = await client.chat.completions.create(
36
- model=self.model_name,
37
- messages=messages,
38
- stream=True,
39
- **kwargs
40
- )
41
-
42
- async for chunk in stream:
43
- content = chunk.choices[0].delta.content
44
- if content is not None:
45
- yield content
 
 
 
 
 
 
 
6
 
7
 
8
  class ChatOpenAI:
9
+ def __init__(self, model_name: str = "gpt-4"):
10
  self.model_name = model_name
11
  self.openai_api_key = os.getenv("OPENAI_API_KEY")
12
  if self.openai_api_key is None:
13
  raise ValueError("OPENAI_API_KEY is not set")
14
+ self.max_tokens = 8192 # Maximum tokens for response
15
+ self.max_total_tokens = 16384 # Maximum total tokens (prompt + response)
16
 
17
  def run(self, messages, text_only: bool = True, **kwargs):
18
  if not isinstance(messages, list):
19
  raise ValueError("messages must be a list")
20
 
21
  client = OpenAI()
22
+ try:
23
+ response = client.chat.completions.create(
24
+ model=self.model_name,
25
+ messages=messages,
26
+ max_tokens=self.max_tokens,
27
+ temperature=0.7, # Add some creativity while maintaining accuracy
28
+ **kwargs
29
+ )
30
+ if text_only:
31
+ return response.choices[0].message.content
32
+ return response
33
+ except Exception as e:
34
+ print(f"Error in chat completion: {str(e)}")
35
+ raise
36
 
37
  async def astream(self, messages, **kwargs):
38
  if not isinstance(messages, list):
 
40
 
41
  client = AsyncOpenAI()
42
 
43
+ try:
44
+ stream = await client.chat.completions.create(
45
+ model=self.model_name,
46
+ messages=messages,
47
+ max_tokens=self.max_tokens,
48
+ temperature=0.7, # Add some creativity while maintaining accuracy
49
+ stream=True,
50
+ **kwargs
51
+ )
52
+
53
+ async for chunk in stream:
54
+ content = chunk.choices[0].delta.content
55
+ if content is not None:
56
+ yield content
57
+ except Exception as e:
58
+ print(f"Error in chat completion stream: {str(e)}")
59
+ raise
chainlit.md CHANGED
@@ -1,3 +1,14 @@
1
- # Welcome to Chat with Your Text/PDF File
2
 
3
- With this application, you can chat with an uploaded text/PDFfile that is smaller than 2MB!
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Welcome to Chat with Your Text/PDF File! 📚
2
 
3
+ This application allows you to chat with any text or PDF file (up to 2MB) using AI. Here's how to use it:
4
+
5
+ 1. Upload your file (PDF or text)
6
+ 2. Wait for processing
7
+ 3. Start asking questions about the content
8
+
9
+ The AI will search through your document and provide relevant answers based on the content.
10
+
11
+ ## Tips
12
+ - For best results, use clear and specific questions
13
+ - The AI works best with well-formatted documents
14
+ - You can ask follow-up questions about the content