forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolveMathProblem.swift
More file actions
34 lines (28 loc) · 917 Bytes
/
SolveMathProblem.swift
File metadata and controls
34 lines (28 loc) · 917 Bytes
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
import Foundation
import LangChain
import PythonKit
func solveMathProblem(_ problem: String) async throws -> String {
#if DEBUG
let verbose = true
#else
let verbose = false
#endif
struct E: Error, LocalizedError {
var errorDescription: String? {
"Failed to parse answer."
}
}
let task = Task {
try withReadableThrowingPython {
let langchain = try Python.attemptImport("langchain")
let LLMMathChain = langchain.LLMMathChain
let llm = try LangChainChatModel.DynamicChatOpenAI(temperature: 0)
let llmMath = LLMMathChain.from_llm(llm, verbose: verbose)
let result = try llmMath.run.throwing.dynamicallyCall(withArguments: problem)
let answer = String(result)
if let answer { return answer }
throw E()
}
}
return try await task.value
}