Skip to content

Commit ddff302

Browse files
Merge pull request #140 from allenai/better-markdown
Add markdown support to bot replies
2 parents dc07d50 + 3c16aa1 commit ddff302

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

OLMoE.swift.xcodeproj/project.pbxproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
48952DF02D4D69D400476373 /* MarkdownUI in Frameworks */ = {isa = PBXBuildFile; productRef = 48952DEF2D4D69D400476373 /* MarkdownUI */; };
1011
489710A22D14D22600F0DB43 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 489710A12D14D22600F0DB43 /* README.md */; };
1112
489FD75C2CF13E4E0011E908 /* AttestManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 489FD75B2CF13E4E0011E908 /* AttestManager.swift */; };
1213
48B806352CF54D9F00E1CC0A /* LambdaResponseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48B806342CF54D9600E1CC0A /* LambdaResponseModel.swift */; };
@@ -68,6 +69,7 @@
6869
buildActionMask = 2147483647;
6970
files = (
7071
A0117FFF2C990F5600035007 /* llama in Frameworks */,
72+
48952DF02D4D69D400476373 /* MarkdownUI in Frameworks */,
7173
);
7274
runOnlyForDeploymentPostprocessing = 0;
7375
};
@@ -167,6 +169,7 @@
167169
name = OLMoE.swift;
168170
packageProductDependencies = (
169171
A0117FFE2C990F5600035007 /* llama */,
172+
48952DEF2D4D69D400476373 /* MarkdownUI */,
170173
);
171174
productName = OLMoE.swift;
172175
productReference = A0117FE82C990EAB00035007 /* OLMoE.swift.app */;
@@ -201,6 +204,7 @@
201204
mainGroup = A0117FDF2C990EAB00035007;
202205
packageReferences = (
203206
A0117FFD2C990F5600035007 /* XCRemoteSwiftPackageReference "llama" */,
207+
48952DEE2D4D69D400476373 /* XCRemoteSwiftPackageReference "swift-markdown-ui" */,
204208
);
205209
productRefGroup = A0117FE92C990EAB00035007 /* Products */;
206210
projectDirPath = "";
@@ -484,6 +488,14 @@
484488
/* End XCConfigurationList section */
485489

486490
/* Begin XCRemoteSwiftPackageReference section */
491+
48952DEE2D4D69D400476373 /* XCRemoteSwiftPackageReference "swift-markdown-ui" */ = {
492+
isa = XCRemoteSwiftPackageReference;
493+
repositoryURL = "https://github.com/gonzalezreal/swift-markdown-ui";
494+
requirement = {
495+
kind = upToNextMajorVersion;
496+
minimumVersion = 2.4.1;
497+
};
498+
};
487499
A0117FFD2C990F5600035007 /* XCRemoteSwiftPackageReference "llama" */ = {
488500
isa = XCRemoteSwiftPackageReference;
489501
repositoryURL = "https://github.com/ggerganov/llama.cpp/";
@@ -495,6 +507,11 @@
495507
/* End XCRemoteSwiftPackageReference section */
496508

497509
/* Begin XCSwiftPackageProductDependency section */
510+
48952DEF2D4D69D400476373 /* MarkdownUI */ = {
511+
isa = XCSwiftPackageProductDependency;
512+
package = 48952DEE2D4D69D400476373 /* XCRemoteSwiftPackageReference "swift-markdown-ui" */;
513+
productName = MarkdownUI;
514+
};
498515
A0117FFE2C990F5600035007 /* llama */ = {
499516
isa = XCSwiftPackageProductDependency;
500517
package = A0117FFD2C990F5600035007 /* XCRemoteSwiftPackageReference "llama" */;

OLMoE.swift.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OLMoE.swift/Views/ChatView.swift

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
import SwiftUI
10+
import MarkdownUI
1011

1112
public struct UserChatBubble: View {
1213
var text: String
@@ -43,11 +44,28 @@ public struct BotChatBubble: View {
4344
if isGenerating && text.isEmpty {
4445
TypingIndicator()
4546
} else {
46-
Text(text)
47+
Markdown(text)
4748
.padding(.top, -2)
4849
.background(Color("BackgroundColor"))
4950
.frame(maxWidth: maxWidth * 0.75, alignment: .leading)
5051
.font(.body())
52+
.markdownTextStyle(\.code) {
53+
FontFamilyVariant(.monospaced)
54+
FontSize(.em(0.85))
55+
BackgroundColor(Color("Surface").opacity(0.35))
56+
}
57+
.markdownBlockStyle(\.codeBlock) { configuration in
58+
configuration.label
59+
.frame(maxWidth: .infinity, alignment: .leading)
60+
.padding(.horizontal, 12)
61+
.padding(.vertical, 6)
62+
.background(Color("Surface").opacity(0.35))
63+
.markdownTextStyle {
64+
FontFamilyVariant(.monospaced)
65+
FontSize(.em(0.85))
66+
}
67+
.markdownMargin(top: 8, bottom: 8)
68+
}
5169
}
5270
Spacer()
5371
}
@@ -296,6 +314,106 @@ public struct ChatView: View {
296314
}
297315
}
298316

317+
#Preview("Markdown Features") {
318+
let exampleHistory: [Chat] = [
319+
Chat(role: .user, content: "Show me what markdown features you support"),
320+
Chat(role: .bot, content: """
321+
# Markdown Showcase
322+
Let me demonstrate the various markdown features I support:
323+
324+
## Text Formatting
325+
You can use **bold**, *italic*, or ***both***. You can also ~~strikethrough~~ text.
326+
327+
## Code Examples
328+
Here's a code sample:
329+
```swift
330+
struct Example {
331+
let value: String
332+
func process() -> String {
333+
return "Processed: \\(value)"
334+
}
335+
}
336+
```
337+
You can also use `inline code` or ``nested `backticks` ``.
338+
339+
## Lists and Tasks
340+
Here's what I can do:
341+
1. Ordered lists
342+
* Nested bullets
343+
* Deep nesting
344+
345+
And task lists:
346+
- [x] Completed task
347+
- [ ] Pending task
348+
- [x] Nested task
349+
"""),
350+
351+
Chat(role: .user, content: "That's cool! What about tables and quotes?"),
352+
Chat(role: .bot, content: """
353+
## Tables
354+
| Feature | Support | Notes |
355+
|---------|:-------:|-------|
356+
| Tables | ✅ | With alignment |
357+
| Lists | ✅ | Nested too |
358+
359+
## Blockquotes
360+
> Single quote
361+
>> Nested quote
362+
>>> Triple nested with **bold** and *italic*
363+
364+
## Links and References
365+
[External Link](https://example.com)
366+
<https://auto-link.com>
367+
368+
Reference-style [link][ref] and footnotes[^1]
369+
370+
[^1]: This is a footnote
371+
[ref]: https://example.com
372+
"""),
373+
374+
Chat(role: .user, content: "Any special features?"),
375+
Chat(role: .bot, content: """
376+
## Special Elements
377+
<details>
378+
<summary>Expandable Section</summary>
379+
380+
* Hidden content
381+
* More items
382+
</details>
383+
384+
## Math and Diagrams
385+
Math: $E = mc^2$
386+
387+
```mermaid
388+
graph TD;
389+
A-->B;
390+
B-->C;
391+
C-->D;
392+
```
393+
394+
## Definition Lists
395+
Term 1
396+
: First definition
397+
: Another definition
398+
399+
Term 2
400+
: With nested list
401+
* Item 1
402+
* Item 2
403+
""")
404+
]
405+
406+
ChatView(
407+
history: exampleHistory,
408+
output: "",
409+
isGenerating: .constant(false),
410+
isScrolledToBottom: .constant(true),
411+
stopSubmitted: .constant(false)
412+
)
413+
.padding(12)
414+
.background(Color("BackgroundColor"))
415+
}
416+
299417
#Preview("Replying") {
300418
let exampleOutput = "This is a bot response that spans multiple lines to better test spacing and alignment in the chat view during development previews in Xcode. This is a bot response that spans multiple lines to better test spacing and alignment in the chat view during development previews in Xcode."
301419
let exampleHistory: [Chat] = [

0 commit comments

Comments
 (0)